我使用上下文变量从 onReceive() 方法开始了一个带有对话框主题的活动。启动的活动具有带有确定和取消按钮的警报对话框布局。当用户点击确定/取消按钮时,我正在调用 finish() 方法,因此活动被破坏。但是当我启动应用程序时,这个活动又来了。这是因为使用上下文变量启动活动。即使我设置了 FLAG_ACTIVITY_NEW_TASK,它也会在我启动应用程序时一次又一次地出现。有人可以帮助我如何避免这种活动。
编辑
public class C2DMMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) {
Intent customDialogIntent = new Intent(context,DialogActivity.class);
customDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(customDialogIntent);
}
}
}
public class DialogActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog);
TextView textAlertTitle = (TextView)findViewById(R.id.textAlertTitle);
TextView textAlertMessage = (TextView)findViewById(R.id.textAlertMessage);
Button button1Alert = (Button)findViewById(R.id.button1Alert);
Button button2Alert = (Button)findViewById(R.id.button2Alert);
textAlertTitle.setText("Notification");
textAlertMessage.setText("One notification is waiting for you. Do you want to see the notification?");
button1AlertOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(DialogActivity.this,HomeActivity.class)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity();
}
});
button2AlertCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
显现
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.ww.activity" android:versionCode="1" android:versionName="1.5">
<uses-sdk android:minSdkVersion="8" />
<permission android:name="com.sample.ww.activity.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sample.ww.activity.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="SplashActivity" android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"></category>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</activity>
<activity android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" android:name="HomeActivity"></activity>
<activity android:theme="@style/AlertDialog.NoTitleBar"
android:screenOrientation="portrait"
android:name="DialogActivity"
android:launchMode="singleInstance"
android:taskAffinity="com.sample.ww.activity">
</activity>
<receiver android:name="com.nielsen.ww.receiver.C2DMMessageReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.sample.ww.activity" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.sample.ww.activity" />
</intent-filter>
</receiver>