2

アプリケーションをバックグラウンドで実行していることをユーザーに通知する通知を作成する方法を教えてください。

4

3 に答える 3

1

onPause()アクティビティのメソッドをオーバーライドする必要がありますnotification

通知については、こちらを参照してください。また、ここからアクティビティのライフサイクルを確認できます。

于 2012-11-29T11:48:32.617 に答える
0

onPause()andメソッドはonResume()、バックグラウンドおよびフォアグラウンドに戻されたときに呼び出されますが、アプリケーションが初めて起動されたとき、およびアプリケーションが強制終了される前にも呼び出されます。あなたはここでもっと読むことができます:

http://developer.android.com/reference/android/app/Activity.html

これがあなたの質問としての通知の例です。

private static final int MY_NOTIFICATION_ID = 1;
    private NotificationManager notificationManager;
    private Notification myNotification;

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        myNotification = new Notification(R.drawable.ic_action_search,
                "Notification!", System.currentTimeMillis());
        Context context = getApplicationContext();
        String notificationTitle = "Exercise of Notification!";
        String notificationText = "http://android-er.blogspot.com/";
        Intent myIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse("www.google.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(
                MainActivity.this, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
        myNotification.defaults |= Notification.DEFAULT_SOUND;
        myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        myNotification.setLatestEventInfo(context, notificationTitle,
                notificationText, pendingIntent);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

    }
于 2012-11-29T12:01:39.893 に答える
0

onPause() と onResume() ではなく、onStop() と onStart() でこれを行うことをお勧めします。そうしないと、アクティビティの上に何かが表示されるたびに通知が呼び出されますが、アクティビティは引き続き表示されます (たとえば、SMS を受信したためにポップアップが表示されます...)。アクティビティの前にポップアップが表示されます。 . その場合、onStop() ではなく、onPause() が呼び出されます。

于 2012-11-29T13:05:07.047 に答える