My notification service starts Main activity if my app has been minimised by pressing the home button
My app has a notification service. The problem is when my app has been minimised [by pressing the home button] and notification service fires a notification my main activity gets started on its own. Its really annoying and i need to fix it.
Any one with a probable solution??
I tried using android:launchMode="singleInstance" given as a solution @ Why does starting an activity from a widget cause my main activity to start as well? but the problem with this solution is that if i try to re-start my app using the "long press home button, which gives the list of recently used app" my ap开发者_C百科p never runs.
In order to re-run it then i have to go to my App's Icon in menu and open it from there.
Is there a way i can solve both these problems??
Activity that generates notification :
package org.example.com;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class ParentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_copy);
NotificationManager newNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notificationDetails = new Notification(R.drawable.icon,"Downloads available!!",System.currentTimeMillis());
notificationDetails.flags = Notification.FLAG_AUTO_CANCEL;
Intent newIntent = new Intent(this,ParentActivity.class);
newIntent.putExtra("IS_FOREGROUND",1);
//newIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent newPendingIntent = PendingIntent.getActivity(this, 0, newIntent, 0);
CharSequence contentTitle = "Downloads Available!!";
CharSequence contentText = "Some new Downloads are available, click to enable.";
int notificationId = 3;
notificationDetails.setLatestEventInfo(this, contentTitle, contentText, newPendingIntent);
newNotificationManager.notify(notificationId, notificationDetails);
finish();
}
}
I see in your code that your are in an activity, that's why your application come to the foreground I think. Does your service start this activity? If it is a service that fires the notification, why not creating the notification in that service? Then your application will stay in the background.
精彩评论