The way I solved that problem was by assigning a unique requestCode when you get the PendingIntent:
PendingIntent.getActivity(context, requestCode, showIntent, 0);
By doing so you are registering with the system different/unique intent instances.
Tip: A good way of making the requestCode unique would be by passing to it the current system time.
int requestID = (int) System.currentTimeMillis();
(Original answer revised, see edit history for it)
I'm not actually sure, which part of your system is malfunctioning so I post my test code here, which I tested to work correctly. First there is MyService
, then TestActivity
which displays the car details in DetailsActivity
:
CarService.java
public class CarService extends IntentService
{
public CarService()
{
super("CarService");
}
protected void onHandleIntent(Intent intent)
{
Bundle extras = intent.getExtras();
if (extras == null)
{
Log.e("CarService", "Service onHandleIntent null extras");
return;
}
int carId = extras.getInt("carId");
String carName = extras.getString("name");
Log.i("CarService", "Service onHandleIntent car = " + carName + " with ID = " + Integer.toString(carId));
Intent notificationIntent;
notificationIntent = new Intent(this, DetailsActivity.class);
notificationIntent.putExtra("carId", carId);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pending = PendingIntent.getActivity(this, carId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notif = new Notification(R.drawable.icon, carName, System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.setLatestEventInfo(getApplicationContext(), carName, "Car Details", pending);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(carId, notif);
}
}
TestActivity.java (your main activity)
public class TestActivity extends Activity implements OnClickListener
{
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Button btn = (Button) findViewById(R.id.launch);
btn.setOnClickListener(this);
}
@Override public void onClick(View v)
{
startActivity(new Intent(this, DetailsActivity.class));
}
}
test.xml (layout for TestActivity.java)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" a:id="@+id/layout_root" a:orientation="vertical" a:layout_width="fill_parent" a:layout_height="fill_parent">
<TextView a:id="@+id/test_value" a:text="Main..." a:layout_width="wrap_content" a:layout_height="wrap_content"/>
<Button a:id="@+id/launch" a:text="Details" a:layout_width="100dp" a:layout_height="wrap_content"/>
</LinearLayout>
DetailsActivity.java (car details listed here + launches CarService + notifications lead back to here)
public class DetailsActivity extends Activity implements OnClickListener
{
private String[] cars = new String[]{"Ferrari", "Lamborghini", "Lada", "Nissan", "Opel", "Bugatti"};
private int id = 0;
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
Button btn = (Button) findViewById(R.id.doit);
btn.setOnClickListener(this);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
final int id = extras.getInt("carId");
Log.i("DetailsActivity", "Car ID: " + id);
TextView tv = (TextView) findViewById(R.id.test_value);
tv.setText("Car ID = " + Integer.toString(id) + ", which is " + cars[id%6]); // id%6 prevents a crash with the string array when clicking test button over 6 times
}
}
@Override public void onClick(View v)
{
Intent intent = new Intent(this, CarService.class);
intent.putExtra("carId", id);
intent.putExtra("name", cars[id%6]); // id%6 prevents a crash with the string array when clicking test button over 6 times
startService(intent);
++id;
}
}
details.xml (layout for TestActivity.java)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" a:orientation="vertical" a:layout_width="fill_parent" a:layout_height="fill_parent">
<TextView a:id="@+id/test_value" a:text="No details yet: click the button." a:layout_width="wrap_content" a:layout_height="wrap_content"/>
<Button a:id="@+id/doit" a:text="Test" a:layout_width="100dp" a:layout_height="wrap_content"/>
</LinearLayout>
I hope all that works correctly as I did make some minor changes on the fly.
Best Answer
Yes, it does.
This is precisely what the documentation says is supposed to happen.
Option #1: Put a different action string in each of your
Intents
. This will make them different (from the standpoint offilterEquals()
) and give them separatePendingIntents
. However, since you are specifying the component in theIntent
(MainApplication.class
), the action will not affect how theIntent
is routed.Option #2: Use a different
requestCode
(2nd parameter) on yourgetActivity()
calls. While this is documented as "currently not used", it does result in differentPendingIntent
objects being returned. However, since this behavior is undocumented, it may change in the future.