jwh*_*els 6 android dialog android-activity
我正在编写一个程序,在收到短信时提供快速回复对话框.
但是,我得到了意想不到的结果.当我收到一条短信时,会出现相应的对话框活动,显示正确的电话号码和消息,但是它背后有第二个活动是我程序中的"默认"活动(这是我启动应用程序时打开的活动)
我不希望第二项活动出现.快速回复活动应该在用户以前做过的任何事情之上自己出现.
'浮动'活动:
public class quickReply extends Activity {
String mNumber, mMessage;
TextView mMainText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainText = (TextView)findViewById(R.id.mainText);
try{
Intent i = getIntent();
Bundle extras = i.getExtras();
mNumber = extras.getString("theNumber");
mMessage = extras.getString("theMessage");
this.setTitle("Message From:" + mNumber);
mMainText.setText(mMessage);
} catch(Exception e) {
mMainText.setText(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
}
对onReceive()内部活动的调用
Intent i = new Intent(context, quickReply.class);
i.putExtra("theNumber", mNumber);
i.putExtra("theMessage", mMessage);
i.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Run Code Online (Sandbox Code Playgroud)
清单:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".quickReply"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
Run Code Online (Sandbox Code Playgroud)
我发现唯一可行的方法是在清单中的活动定义中:
android:launchMode="singleInstance"
Run Code Online (Sandbox Code Playgroud)
但是一旦对话框关闭,您就必须重新启动主要/默认活动。注意:您将丢失上次启动时的所有状态,因此这是一个不太理想的解决方案。
更新:
您还可以通过以下方式执行此操作:
Intent.FLAG_ACTIVITY_CLEAR_TASK
Run Code Online (Sandbox Code Playgroud)
所以这就是我所做的:
当用户关闭对话框时,使用 onCreate() 中处理的额外意图 (IS_BACK) 再次启动 main 并调用:
moveTaskToBack(真);
这将使对话框中的任务保持在顶部,而主任务保持在堆栈的后面。
| 归档时间: |
|
| 查看次数: |
3390 次 |
| 最近记录: |