我的应用被杀后,通知仍然停留

时间:2016-07-12 02:12:23

标签: android notifications android-notifications

我在AsyncTask中有一个静态媒体播放器 当onPause()被调用时,我创建了一个通知

if(Reproduce.mPlayer!=null && Reproduce.mPlayer.isPlaying()  ){
nPanel = new Controler(getApplicationContext());
}

调用onResume()时,通知会消失。

if(nPanel != null) {
        nPanel.notificationCancel();
    }

到目前为止一切顺利!

但如果我通过滑动通知停止我的应用程序,我不想要这个,我没有办法避免这种情况。

Pd:如果我从app杀死应用程序>强制杀死 - 通知消失

  

这是我的通知代码

public class Controler {

private Context parent;
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;

public Controler(Context parent) {
    // TODO Auto-generated constructor stub
    this.parent = parent;
    nBuilder = new NotificationCompat.Builder(parent)
            .setContentTitle("Parking Meter")
            .setSmallIcon(R.drawable.stock)
            .setOngoing(true);

    remoteView = new RemoteViews(parent.getPackageName(), R.layout.notificationview);

    //set the button listeners
    setListeners(remoteView);
    nBuilder.setContent(remoteView);

    nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
    nManager.notify(2, nBuilder.build());

  }

public void setListeners(RemoteViews view){
    //listener 1

    Intent volume = new Intent(parent,NotificationReturnSlot.class);
    volume.putExtra("DO", "volume");
    PendingIntent btn1 = PendingIntent.getActivity(parent, 0, volume, 0);
    view.setOnClickPendingIntent(R.id.btn1, btn1);

    //listener 2
    Intent stop = new Intent(parent, NotificationReturnSlot.class);
    stop.putExtra("DO", "stop");
    PendingIntent btn2 = PendingIntent.getActivity(parent, 1, stop, 0);
    view.setOnClickPendingIntent(R.id.btn2, btn2);

  }

public void notificationCancel() {
    nManager.cancel(2);
   }
  }

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


    String action = (String) getIntent().getExtras().get("DO");
    NotificationManager nMgr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(2);
    try{if (action.equals("volume")) {
        if(Reproduce.mPlayer != null) {
            if (Reproduce.mPlayer.isPlaying()) {
                Reproduce.mPlayer.stop();


                finish();
            }
        }

        //Your code
    } else if (action.equals("stop")) {
        //Your code
        Log.i("help", "stopNotification");
        try {
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
            //  the activity from a service
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            startActivity(intent);
        }catch (Exception e ){}
    }
    finish();
}catch (Exception e){}

 }
  

和AndroidManifest.xml

        <activity android:name=".NotificationReturnSlot"
        android:launchMode="singleTask"
        android:taskAffinity=""
        android:excludeFromRecents="true"/>

onPause()将无效,因为它会在每次调用时终止通知并且永远不会调用onDestroyd()

0 个答案:

没有答案