开发者

BroadcastReceiver how to start new intent

开发者 https://www.devze.com 2023-04-10 10:17 出处:网络
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.

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 **","noConnect开发者_C百科ivity " + 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号