BroadcastReceiver how to start new intent

自作多情 提交于 2019-12-10 06:08:51

问题


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);

回答1:


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?



来源:https://stackoverflow.com/questions/7653279/broadcastreceiver-how-to-start-new-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!