国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

在BroadcastReceiver中啟動Activity:

2019-11-09 14:07:44
字體:
供稿:網(wǎng)友
 在BroadcastReceiver中啟動Activity:    如果在BroadcastReceiver的onReceive()方法中如下啟動一個Activity  Intent intent=new Intent(context,AnotherActivity.class);  context.startActivity(intent);  可捕獲異常信息:  android.util.AndroidRuntimeException:   Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.   Is this really what you want?  它說明:在Activity的context(上下文環(huán)境)之外調(diào)用startActivity()方法時  需要給Intent設(shè)置一個flag:FLAG_ACTIVITY_NEW_TASK    所以在BroadcastReceiver的onReceive()方法中啟動Activity應(yīng)寫為:  Intent intent=new Intent(context,AnotherActivity.class);  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  context.startActivity(intent);      之前描述了問題的現(xiàn)象和解決辦法,現(xiàn)在試著解釋一下原因:  1 在普通情況下,必須要有前一個Activity的Context,才能啟動后一個Activity  2 但是在BroadcastReceiver里面是沒有Activity的Context的  3 對于startActivity()方法,源碼中有這么一段描述:    Note that if this method is being called from outside of an    {@link android.app.Activity} Context, then the Intent must include    the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag.  This is because,    without being started from an existing Activity, there is no existing    task in which to place the new activity and thus it needs to be placed    in its own separate task.    說白了就是如果不加這個flag就沒有一個Task來存放新啟動的Activity.      4 其實該flag和設(shè)置Activity的LaunchMode為SingleTask的效果是一樣的
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 崇义县| 万载县| 休宁县| 明溪县| 鄱阳县| 岑溪市| 冷水江市| 朝阳县| 阳城县| 盐池县| 广河县| 太白县| 南昌县| 乌鲁木齐市| 湛江市| 沙湾县| 措勤县| 高雄县| 福贡县| 饶平县| 昌邑市| 高州市| 威宁| 红原县| 郯城县| 高阳县| 栾城县| 山东省| 玛曲县| 康乐县| 沐川县| 内江市| 大方县| 平远县| 吉林市| 句容市| 泽普县| 五家渠市| 张家口市| 阿城市| 阿城市|