现在的位置: 首页 > 综合 > 正文

Service启动Activity并将该Activity设置为dialog

2018年04月19日 ⁄ 综合 ⁄ 共 1050字 ⁄ 字号 评论关闭

场景:当Service在后台下载数据,最前端可能显示的是其他应用的Activity或者是在播放视屏,这时,当Service后台下载完成时,需要弹出一个下载完成提示对话框。

实现思路:当Service下载完成时,启动Activity。

1)、 //用于启动DownLoadok Dialog

IntentdialogIntent = newIntent(DownLoadService.this, DownOkDialog.class);

StringdialogStr[] = {str[1].concat(str[2]),str[5]};

dialogIntent.putExtra("downloadok", dialogStr);//要传递的附件信息

dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

DownLoadService.this.startActivity(dialogIntent);

说明:这里的dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  若不进行该设置,会报错:Calling startActivity() from outsideof an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is thisreally what
you want?

还要注意DownLoadService.this.startActivity(dialogIntent);这一句的上下文,不然acticity的history会乱套。

2)、Activity设置

在manifest文件中设置       

<activity

            android:name="com.example.upgrade.DownOkDialog"

            android:theme="@android:style/Theme.Dialog"

            ></activity>

在onCreat()中设置隐藏Activity的标题明, 注意一定要在setContentView之前设置。  

  this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.down_ok_dialog_layout);

以上内容均是自己学习中遇到的知识点,做一个简单总结。

抱歉!评论已关闭.