バックグラウンドを含めて、アプリケーションが実行されているときにステータスバーにアイコンを表示する方法は?
-
09-10-2019 - |
質問
バックグラウンドで実行されているときを含め、アプリケーションが実行されているときに、ステータスバーにアイコンを置きたいです。これどうやってするの?
解決
通知と通知マネージャーでこれを行うことができるはずです。ただし、アプリケーションが実行されていないことを知るための保証された方法を取得することは難しい部分です。
次のようなことをすることで、あなたが望んでいることの基本的な機能を取得できます。
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()メソッドに。
しかし、その後、物事は難しいです。アプリケーションの殺害はいつでも発生する可能性があります。そのため、アプリケーションクラスのOnterminate()に何かを入れることもできますが、呼び出されることは保証されていません。
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);
アイコンを削除するために必要なものになります。
他のヒント
新しい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);
開発ガイドを見てください」ステータスバー通知の作成".
アプリケーションが実行されているときにのみアイコンを保持するという目標を達成する1つの方法は、通知を初期化することです 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);
}
}
それは実際に動作します。上記の例からメソッドを作成しました。
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);