2

バックグラウンドで実行されているサービスがあり、新しいデータがある場合にユーザーに通知したいのですが、通知マネージャーを使用しましたが、通知が表示されますが、クリックしても何もしません (表示されるはずです)私はAndroidで初めてのアクティビティで、ここに私のコードがあります

NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = newNotification(R.drawable.shoppingcarticon, "Nouvelle notification de ShopAlert", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(this,GooglemapActivity.class);
PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);

notification.setLatestEventInfo(this, "This is the title", "This is the text", activity);
notification.number += 1;
notificationmanager.notify(0, notification);

あなたの助けに感謝します。

4

2 に答える 2

5

このコードを使用できます。Main.Class をアクティビティ クラスに変更し、必要に応じてタイトルとコンテンツ テキストを変更します。

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);


int icon = R.drawable.ic_action_search;
CharSequence tickerText = "Pet Parrot";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "Hungry!";
CharSequence contentText = "your parrot food meter is: ";
Intent notificationIntent = new Intent(context, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

mNotificationManager.notify(1, notification); // Log.d("test", "Saving Data to File from Service.");
于 2012-07-22T10:28:49.230 に答える
3

次の行:

PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);

このように見えるはずです:

PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);

Activityではなくを開始しようとしているためですService。お役に立てれば。

于 2012-07-22T10:20:41.047 に答える