Androidでの通知に問題があります。何が間違っているのかわかりません...この通知はサービスクラス(AfterBootService)で作成され、以下のレシーバークラス(MyReceiver)コードで起動が完了するとサービスが起動します。両方のクラス:
MyReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AfterBootService.class);
context.startService(service);
}
}
そしてここAfterBootService.class
public class AfterBootService extends Service {
private NotificationManager mNotificationManager;
private static final int NOTIFICATION_ID = 1;
@Override
public void onCreate() {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
mNotificationManager.cancel(NOTIFICATION_ID);
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* Show a notification while this service is running.
*/
private void showNotification() {
int icon = R.drawable.icon_notification_ribbon;
String tickerText = "Your Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
String expandedText = "Sample Text";
String expandedTitle = "Program Name Here";
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, IconActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
//notification.flags |= Notification.FLAG_NO_CLEAR;
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(context, expandedTitle, expandedText, contentIntent);
// Send the notification.
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
そして今、私がすべてを起動すると、受信機はサービスを開始し、サービスは通知を開始しますが、ステータスバーに表示され、通知ウィンドウに適切なメッセージが表示された後、数秒後にITDISAPPEAR...何も設定しませんでしたそれはそのメッセージが消えることを可能にします(私は専門家ではないと思います)。私が知りたいこと:
- サービスの実行中にその通知メッセージを常に保持するにはどうすればよいですか?
- その通知を開始したサービスを実行せずに、常に通知を保持することは可能ですか?より注意深く説明すると、サービスで通知を起動した後、stopSelf()でサービスを破棄し、その通知メッセージを保持することができますか?
必要に応じて、ここにマイマニフェストを貼り付けることができます。どうも