Android GCM 启动特定 Activity 失败
Android GCM start specific Activity fails
我有这段代码可以从 GCM 服务内部生成通知:
private void sendNotification(Bundle extras) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String notification_type = extras.getString("type");
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, extras.getString("message"), when);
String title = this.getString(R.string.app_name);
Intent notificationIntent = new Intent(this, InvitationActivity.class);
if ( notification_type != null && notification_type.equals("invitation"))
{
notificationIntent = new Intent(this, InvitationActivity.class);
notificationIntent.putExtra("lobby_id", Integer.valueOf(extras.getString("messagedata", "0")));
} else {
notificationIntent = new Intent(this, MainActivity.class);
}
// set intent so it does not start a new activity
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
// Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, title, extras.getString("message"), intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
问题是如果我点击通知,MainActivity 就会启动,但是如果
notificationIntent = new Intent(this, InvitationActivity.class);
被调用,我点击通知,似乎什么都没发生,没有错误,没有 activity 被启动。这只发生在 InvitationActivity 中,Mainactivity 工作正常。
邀请活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invitation);
lobby_id = getIntent().getIntExtra("lobby_id", 0);
编辑:
AndroidManifest.xml:
<activity
android:name=".activities_fragments.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities_fragments.InvitationActivity"
android:label="@string/title_activity_invitation"
android:theme="@android:style/Theme.Holo.Dialog">
</activity>
奇怪的是,如果我从 MainActivity 中启动 InvitationActivity,它会按预期工作,但不是从 GCMService 内部启动。
添加
android:exported="true"
在您的 invitationActivity 标签内,如下所示:
<activity
android:name=".activities_fragments.InvitationActivity"
android:label="@string/title_activity_invitation"
android:exported="true"
android:theme="@android:style/Theme.Holo.Dialog">
</activity>
这将解决您的问题
我有这段代码可以从 GCM 服务内部生成通知:
private void sendNotification(Bundle extras) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String notification_type = extras.getString("type");
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, extras.getString("message"), when);
String title = this.getString(R.string.app_name);
Intent notificationIntent = new Intent(this, InvitationActivity.class);
if ( notification_type != null && notification_type.equals("invitation"))
{
notificationIntent = new Intent(this, InvitationActivity.class);
notificationIntent.putExtra("lobby_id", Integer.valueOf(extras.getString("messagedata", "0")));
} else {
notificationIntent = new Intent(this, MainActivity.class);
}
// set intent so it does not start a new activity
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
// Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, title, extras.getString("message"), intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
问题是如果我点击通知,MainActivity 就会启动,但是如果
notificationIntent = new Intent(this, InvitationActivity.class);
被调用,我点击通知,似乎什么都没发生,没有错误,没有 activity 被启动。这只发生在 InvitationActivity 中,Mainactivity 工作正常。
邀请活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invitation);
lobby_id = getIntent().getIntExtra("lobby_id", 0);
编辑: AndroidManifest.xml:
<activity
android:name=".activities_fragments.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities_fragments.InvitationActivity"
android:label="@string/title_activity_invitation"
android:theme="@android:style/Theme.Holo.Dialog">
</activity>
奇怪的是,如果我从 MainActivity 中启动 InvitationActivity,它会按预期工作,但不是从 GCMService 内部启动。
添加
android:exported="true"
在您的 invitationActivity 标签内,如下所示:
<activity
android:name=".activities_fragments.InvitationActivity"
android:label="@string/title_activity_invitation"
android:exported="true"
android:theme="@android:style/Theme.Holo.Dialog">
</activity>
这将解决您的问题