广播接收器如何开始新意图 [英] BroadcastReceiver how to start new intent

查看:94
本文介绍了广播接收器如何开始新意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个广播接收器,以块我的应用程序,如果互联网连接丢失。
通过块我的意思是应用程序有打开一个没有网络连接的活动。

这是我的接收器code:

 公共类ConnectivityReceiver扩展广播接收器{@覆盖
公共无效的onReceive(上下文的背景下,意图意图){    布尔noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,FALSE);
    Log.d(**调试**,noConnectivity+ noConnectivity);    如果(noConnectivity){
        //显示没有互联网连接活动
    }
}
}

难道不可能性开始NoInternetConnection.class时noConnectivity ==真??

谢谢!

解决方案:

 意向书I =新意图(背景下,NoInternetConnection.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ⅰ);


解决方案

您应该只需要调用startActivity:

  context.startActivity(新意图(NoInternetConnection.class));

您将需要确保NoInternetConnection活动在您的清单文件注册:

 <活动机器人:名字=。NoInternetConnection>< /活性GT;

什么问题你具体有?

I implemented a broadcast receiver to "block" my app if the internet connection is lost. By block I mean that the app has to open a "No internet connection" activity.

this is my receiver code:

public class ConnectivityReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    Log.d("** Debug **","noConnectivity " + noConnectivity);

    if(noConnectivity){
        //SHOW NO INTERNET CONNECTION ACTIVITY
    }
}
}

Is it possibile to start NoInternetConnection.class when noConnectivity == true??

Thanks!

SOLUTION:

Intent i = new Intent(context, NoInternetConnection.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

解决方案

You should just have to call startActivity:

context.startActivity(new Intent(NoInternetConnection.class));

You will need to make sure the "NoInternetConnection" activity is registered in your manifest file:

<activity android:name=".NoInternetConnection"></activity>

What issues are you having specifically?

这篇关于广播接收器如何开始新意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆