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

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

在BroadcastReceiver中啟動Activity:

2019-11-09 15:04:12
字體:
供稿:網(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設置一個flag:FLAG_ACTIVITY_NEW_TASK    所以在BroadcastReceiver的onReceive()方法中啟動Activity應寫為:  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和設置Activity的LaunchMode為SingleTask的效果是一樣的
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 双柏县| 江都市| 阳原县| 蓝田县| 黄陵县| 罗田县| 博湖县| 吴川市| 尼玛县| 车险| 大关县| 钟祥市| 公安县| 电白县| 佳木斯市| 高尔夫| 开鲁县| 鹤山市| 安远县| 周至县| 墨江| 五寨县| 昌图县| 公安县| 天台县| 延寿县| 林口县| 南汇区| 长白| 玛纳斯县| 白水县| 成都市| 景德镇市| 莱西市| 迭部县| 徐汇区| 循化| 托克逊县| 蕉岭县| 洪湖市| 耒阳市|