3

i have a static media player in a AsyncTask when onPause() is called i create a notification

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

when onResume() is called, the notifications dissapear.

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

so far so good!

but if i kill my app by swiping the notification stays, i dont want this and i not find a way to avoid this.

Pd: if i kill app from app>force kill -the notification disappear

this is my notification code

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){}

 }

and AndroidManifest.xml

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

onPause() will not work because it will kill notification everytime is called and onDestroyd() is never called

DAVIDBALAS1
  • 484
  • 10
  • 31
user5450074
  • 97
  • 1
  • 8
  • To detect app killing by swiping from recents, see this: http://stackoverflow.com/questions/19568315/how-to-handle-code-when-app-is-killed-by-swiping-in-android – DAVIDBALAS1 Jul 12 '16 at 02:15
  • [This](http://stackoverflow.com/questions/12997800/cancel-notification-on-remove-application-from-multitask-panel) should definitely help you. Basically, you need to write your own service and override onTaskRemoved() method to achieve this. – Harsh Pandey Jul 12 '16 at 02:21

0 Answers0