I've got simple app with status bar icon. When user minimize app, status bar icon appear. When user click on that icon, app should resume and continue job. I've tested on android 2.3.4 and it works but on 4.0 android version my app is not resuming and continue job but open new version of my app. How to make universal noticifaction code and don't be worry of android version?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jajko);

    text = (TextView) findViewById(R.id.tvTime);
    play = (Button) findViewById(R.id.butStart);
    miekko = (Button) findViewById(R.id.butMiekko);
    srednio = (Button) findViewById(R.id.butSrednio);
    twardo = (Button) findViewById(R.id.butTwardo);

    miekko.setOnClickListener(this);
    srednio.setOnClickListener(this);
    twardo.setOnClickListener(this);
    play.setOnClickListener(this);

    mp = MediaPlayer.create(Jajko.this, R.raw.alarm);

    showNotification(this);

}

public static void showNotification(Context context) {
    final Intent result_intent = new Intent(context, Jajko.class);

    result_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    TaskStackBuilder stack_builder = TaskStackBuilder.create(context);
    stack_builder.addParentStack(Jajko.class);
    stack_builder.addNextIntent(result_intent);

    PendingIntent pending_intent = stack_builder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    android.support.v4.app.NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(
            context);

    Resources res = context.getResources();
    builder.setContentIntent(pending_intent)
            .setSmallIcon(R.drawable.icon)
            .setLargeIcon(
                    BitmapFactory.decodeResource(res, R.drawable.icon))
            .setTicker("Egg Timer").setWhen(System.currentTimeMillis())
            .setAutoCancel(false).setContentTitle("Egg Timer")
            .setContentInfo("").setContentText("");
    Notification n = builder.build();
    n.flags = Notification.FLAG_ONGOING_EVENT;

    notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIF_ID, n);
}
有帮助吗?

解决方案

The documentation for TaskStackBuilder says that it behaves differently on pre- and post-3.0 Android. I haven't used it for a pending intent, so I'm not sure what to expect with that.

Try completely removing the three lines with the stack builder, and change your pending intent line to this:

PendingIntent pending_intent = PendingIntent.getActivity(getBaseContext(), 
    0, result_intent, PendingIntent.FLAG_UPDATE_CURRENT);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top