29

アプリケーションがバックグラウンドで実行されているときも含め、アプリケーションが実行されているときはいつでも、ステータスバーにアイコンを表示したいと思います。これどうやってするの?

4

4 に答える 4

21

これは、NotificationとNotificationManagerを使用して実行できるはずです。ただし、アプリケーションが実行されていないことを確実に知る方法を得るのは難しい部分です。

次のようなことを行うことで、必要なものの基本的な機能を取得できます。

Notification notification = new Notification(R.drawable.your_app_icon,
                                             R.string.name_of_your_app, 
                                             System.currentTimeMillis());
notification.flags |= Notification.FLAG_NO_CLEAR
                   | Notification.FLAG_ONGOING_EVENT;
NotificationManager notifier = (NotificationManager)
     context.getSystemService(Context.NOTIFICATION_SERVICE);
notifier.notify(1, notification);

このコードは、アプリケーションの起動時に確実に起動される場所にある必要があります。おそらく、アプリケーションのカスタムアプリケーションオブジェクトのonCreate()メソッドにあります。

しかし、その後は注意が必要です。アプリケーションの強制終了はいつでも発生する可能性があります。したがって、ApplicationクラスのonTerminate()にも何かを入れようとすることができますが、呼び出されることが保証されているわけではありません。

((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);

アイコンを削除するために必要なものになります。

于 2010-10-20T03:36:52.637 に答える
9

新しいAPIの場合は、次を使用できますNotificationCompat.Builder -

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle("Title");
Intent resultIntent = new Intent(this, MyActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(NOTIFICATION_ID, notification);

アプリケーションが実行されていて、誰かが手動でアプリケーションを閉じている限り、表示されます。あなたはいつでも電話することによってあなたの通知をキャンセルすることができます-

mNotifyMgr.cancel(NOTIFICATION_ID);
于 2015-11-13T10:56:43.783 に答える
6

開発ガイド「ステータスバー通知の作成」をご覧ください。

アプリケーションの実行中にのみアイコンをそこに保持するという目標を達成する1つの方法は、通知を初期化し、trueが返された場合にのみメソッドonCreate()を呼び出すことです。cancel(int)onPause()isFinishing()

例:

private static final int NOTIFICATION_EX = 1;
private NotificationManager notificationManager;

@Override
public void onCreate() {
    super.onCreate();

    notificationManager = (NotificationManager) 
        getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.notification_icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 
        0, notificationIntent, 0);

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

    notificationManager.notify(NOTIFICATION_EX, notification);
}

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing()) {
        notificationManager.cancel(NOTIFICATION_EX);
    }
}
于 2010-10-20T03:34:18.397 に答える
4

それは実際に動作します。上記の例からメソッドを作成しました。

private void applyStatusBar(String iconTitle, int notificationId) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(iconTitle);
Intent resultIntent = new Intent(this, ActMain.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT;

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notificationId, notification);}

次のように呼び出す必要があります:applyStatusBar( "Statusbar Test"、10);

于 2016-04-12T16:31:25.350 に答える