我的应用是这样描述的:应用会从服务器接收推送信息,并显示在android的通知栏上。当我单击应用程序的通知时,将加载我从服务器获取的 url。但我现在的问题是,我无法将从服务器收到的 url 发送到原始活动。
以下是我运行应用程序时遇到的错误。
04-09 11:25:26.503: E/AndroidRuntime(1030): FATAL EXCEPTION: IntentService[GCMIntentService-810012644757-1]
04-09 11:25:26.503: E/AndroidRuntime(1030): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.app.ContextImpl.startActivity(ContextImpl.java:624)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
04-09 11:25:26.503: E/AndroidRuntime(1030): at com.ketan.demo.GCMIntentService.generateNotification(GCMIntentService.java:118)
04-09 11:25:26.503: E/AndroidRuntime(1030): at com.ketan.demo.GCMIntentService.onMessage(GCMIntentService.java:70)
04-09 11:25:26.503: E/AndroidRuntime(1030): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:179)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.os.Looper.loop(Looper.java:130)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.os.HandlerThread.run(HandlerThread.java:60)
我的代码:
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent().setClass(context.getApplicationContext(), DemoActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
notificationIntent.putExtra("URL", message);
// Launch the new activity and add the additional flags to the intent
context.getApplicationContext().startActivity(notificationIntent);
/*Intent i = new Intent().setClass(context.getApplicationContext(), DemoActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("URL", message);
// Launch the new activity and add the additional flags to the intent
context.getApplicationContext().startActivity(i);*/
}
活动演示
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkNotNull(SERVER_URL, "http://10.0.2.2:8080/gcm-demo-server/");
// checkNotNull(SENDER_ID, "972801588344");
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
web=(WebView) findViewById(R.id.webView1);
if(url==null)
{
web.loadUrl("http://kqxs.vn/trang_chu");
}else
{
web.loadUrl(url);
}
}
我通过以下方式修复了以下内容
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, DemoActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("URL", message);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
但是我还有另一个问题,这是我第一次加载正确 url 的唯一应用程序,从第二次开始,在从服务器接收到新的 url 之后,但我的应用程序第一次加载了 url。我已经尝试了很多次,但仍然得到结果,所以......