0

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...何も設定しませんでしたそれはそのメッセージが消えることを可能にします(私は専門家ではないと思います)。私が知りたいこと:

  1. サービスの実行中にその通知メッセージを常に保持するにはどうすればよいですか?
  2. その通知を開始したサービスを実行せずに、常に通知を保持することは可能ですか?より注意深く説明すると、サービスで通知を起動した後、stopSelf()でサービスを破棄し、その通知メッセージを保持することができますか?

必要に応じて、ここにマイマニフェストを貼り付けることができます。どうも

4

1 に答える 1

2

on destroy メソッドを編集する

から:

    public void onDestroy() {
    mNotificationManager.cancel(NOTIFICATION_ID);
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}

public void onDestroy() {
    super.onDestroy();

}

あなたの通知をキャンセルし、ユーザーがそれをクリアするまで残ります;)

受信者からのサービスなしで通知をトリガーし、追加できます

   notification.flags = Notification.FLAG_ONGOING_EVENT;

クリア不可に設定する

于 2012-08-25T11:59:03.343 に答える