106

サービスを実行していますが、通知を送信したいと思います。残念ながら、通知オブジェクトには、ではなくContext、のようなが必要です。ActivityService

それをバイパスする方法を知っていますか?通知ごとにを作成しようとしましたActivityが、見苦しく、Activityなしで起動する方法が見つかりませんView

4

5 に答える 5

110

両方ともActivity実際Serviceには、あなたの内であなたのextend Contextように使うことができます。thisContextService

NotificationManager notificationManager =
    (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new Notification(/* your notification */);
PendingIntent pendingIntent = /* your intent */;
notification.setLatestEventInfo(this, /* your content */, pendingIntent);
notificationManager.notify(/* id */, notification);
于 2009-07-30T18:43:43.550 に答える
78

ドキュメントからわかるように、このタイプの通知は非推奨です。

@java.lang.Deprecated
public Notification(int icon, java.lang.CharSequence tickerText, long when) { /* compiled code */ }

public Notification(android.os.Parcel parcel) { /* compiled code */ }

@java.lang.Deprecated
public void setLatestEventInfo(android.content.Context context, java.lang.CharSequence contentTitle, java.lang.CharSequence contentText, android.app.PendingIntent contentIntent) { /* compiled code */ }

より良い方法
は、次のような通知を送信できます。

// prepare intent which is triggered if the
// notification is selected

Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
Notification n  = new Notification.Builder(this)
        .setContentTitle("New mail from " + "test@gmail.com")
        .setContentText("Subject")
        .setSmallIcon(R.drawable.icon)
        .setContentIntent(pIntent)
        .setAutoCancel(true)
        .addAction(R.drawable.icon, "Call", pIntent)
        .addAction(R.drawable.icon, "More", pIntent)
        .addAction(R.drawable.icon, "And more", pIntent).build();


NotificationManager notificationManager = 
  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, n); 

上記
のコードには、最小 API レベル 11 (Android 3.0) が必要です。
最小 API レベルが 11 未満の場合は、サポート ライブラリの NotificationCompat クラスを次のように使用する必要があります。

したがって、最小ターゲット API レベルが 4+ (Android 1.6+) の場合は、次を使用します。

    import android.support.v4.app.NotificationCompat;
    -------------
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.mylogo)
                    .setContentTitle("My Notification Title")
                    .setContentText("Something interesting happened");
    int NOTIFICATION_ID = 12345;

    Intent targetIntent = new Intent(this, MyFavoriteActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nManager.notify(NOTIFICATION_ID, builder.build());
于 2013-10-28T08:28:35.297 に答える
7
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void PushNotification()
{
    NotificationManager nm = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0);

    //set
    builder.setContentIntent(contentIntent);
    builder.setSmallIcon(R.drawable.cal_icon);
    builder.setContentText("Contents");
    builder.setContentTitle("title");
    builder.setAutoCancel(true);
    builder.setDefaults(Notification.DEFAULT_ALL);

    Notification notification = builder.build();
    nm.notify((int)System.currentTimeMillis(),notification);
}
于 2015-09-28T02:38:47.957 に答える
1

まあ、私の解決策がベストプラクティスであるかどうかはわかりません。NotificationBuilder私のコードを使用すると、次のようになります。

private void showNotification() {
    Intent notificationIntent = new Intent(this, MainActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(
                this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

マニフェスト:

    <activity
        android:name=".MainActivity"
        android:launchMode="singleInstance"
    </activity>

そしてここでサービス:

    <service
        android:name=".services.ProtectionService"
        android:launchMode="singleTask">
    </service>

本当にsingleTaskatServiceがあるかどうかはわかりませんが、これは私のアプリケーションで適切に機能します...

于 2014-10-02T07:38:13.597 に答える
-3

これらのいずれも機能しない場合は、またはgetBaseContext()の代わりに を試してください。contextthis

于 2016-04-17T16:35:37.393 に答える