![](/img/trans.png)
[英]How to update widget once broadcast intent CONNECTIVITY_CHANGE is received
[英]How to detect Internet Connection State via CONNECTIVITY_CHANGE broadcast
我需要處理我的應用中廣播的連接更改。 一切都很棒,除了廣播時應用程序崩潰。 我在廣播中使用以下代碼:
@Override
public void onReceive(Context context, Intent intent) {
Log.i("NET", "Broadcast started");
Intent startServiceIntent = new Intent(context, NewsService.class);
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if(noConnectivity) {
context.stopService(startServiceIntent);
Toast.makeText(context, "Connection is terminated!", Toast.LENGTH_LONG).show();
Log.i("NET", "Stopped");
}
else {
context.startService(startServiceIntent);
Toast.makeText(context, "Connection is ok!", Toast.LENGTH_LONG).show();
}
}
如果找不到互聯網連接,則此代碼應停止服務,並在找到連接時啟動該服務。
最后,經過2天的搜索,我發現了問題所在,這與廣播所放置的包裹有關, 我在這里找到了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.