Reputation: 1733
Hi i'm facing this problem. I have an application with 2 activities: -Activity A (Main activity) showing a list of cars -If you click on an item list the Activity B is started showing car's details.
From within activity B you can download info related to that car, a service C in charge of the download is started and a notification is added to the notification bar. If you click the notification you are supposed to see Activity B showing details related to that specific car.
My problem is the following: activity B gets an intent with this extra: carID So in onCreate it reads this extra and ask the db for that specific car details. When i call Activity B from activity A everything works fine. But when i call activity B from the notification bar it doesn't. It always get details about the first car i chose. So for example, i download Ferrari details, and then Lamborghini details....
In my notification i will see 2 notifications, but both of them open activity B showing Ferrari details.
This is how i create the notifications inside the Service C:
int icon = icona;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = message;
CharSequence contentText = appName;
Intent notificationIntent;
notificationIntent = new Intent(this, ActivityB.class);
notificationIntent.putExtra("carId", carId);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(NOT_ID, notification);
and this is how i receive the intent in Activity B:
Intent myIntent = getIntent();
appId = myIntent.getIntExtra("carId", 0);
At the beginning i didn't receive any intent at all from the notification.....then i added PendingIntent.FLAG_UPDATE_CURRENT as you can see above and i get it, but it always the first one. I have verified, and i add the correct carId to each intente but i get another one instead......and each time a open the notification this log message appears:
startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=market.finestraprincipale/.ApplicationActivity bnds=[0,101][320,165] (has extras) }
Can someone help me pls....
Upvotes: 2
Views: 8015
Reputation: 5945
(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.
Upvotes: 3