Display an alert or a view on receiving a notification
I was following this tutorial for displaying a 开发者_Python百科notification on an Android device. When I ran the application on the device, an icon appeared on the status bar (as usually it appears on Android device) which is absolutely perfect. But just out of curiosity I wanted to know that can I display an alert or some view with few details when device receives a notification? I want to implement this concept in my next application.
Some sample would greatly help me.A typical kind of pattern would be for you to register a certain part of your application to "receive", or listen for, specific intents. This way, your application can wake up at any arbitrary point in time, look at the calling intent, and decide how to handle it (be it starting the full application, displaying a dialog, or something else). One good thing that goes along with this is to use the AlarmManager to set an alarm that will fire and wake up your application in the future (periodic updates).
Does this answer your question? I've done something like this in my application and I can help you with the code if you like.
EDIT to implement this you would make a java class that extends either BroadcastReceiver or IntentService (depending on if you want the class to run on the ui thread or as a service respectively). In my example below, I have defined my own intent action key, but typically you would check the action with intent.getAction():
public class QueryService extends IntentService {
public final static String SERVICE_NAME = "QueryService";
// incoming flags
public final static String FLAG_ACTION = "R_ACTION";
public final static String FLAG_EVENTS_RETURNED = "R_EVENTS";
public final static String FLAG_EVENTS_ARCHIVED_RETURNED = "R_EVENTS_ARCH";
public final static String FLAG_SHARED_PREFERNCES_RETURNED = "R_PREFS";
// outgoing flags
public final static String RETURN_EVENTS = "RETURN_E";
public final static String RETURN_EVENTS_ARCHIVED = "RETURN_E_A";
public final static String RETURN_SHARED_PREFS = "RETURN_S_P";
public QueryService() {
super(SERVICE_NAME);
}
protected void onHandleIntent(Intent intent) {
String rAction = intent.getStringExtra(FLAG_ACTION);
boolean rEvents = intent.getBooleanExtra(FLAG_EVENTS_RETURNED, true);
boolean rEventsArchived = intent.getBooleanExtra(FLAG_EVENTS_ARCHIVED_RETURNED, true);
boolean rSharedPrefs = intent.getBooleanExtra(FLAG_SHARED_PREFERNCES_RETURNED, true);
if(rAction == null){
Log.e(SERVICE_NAME, "no return action specified, exiting...");
return;
}
Log.i(SERVICE_NAME, "Caller: " + rAction);
DroidTaskApplication app = (DroidTaskApplication)getApplicationContext();
Intent resultsIntent = new Intent(rAction);
// TODO assembling events / archived events from a database needs
// a function that gets the complete event instead of just its headers
// assemble event objects and insert them
if(rEvents){
List<Event> liteEvents = app.edo.getAllEvents();
if(liteEvents != null){
for(Event e : liteEvents){
int id = e.getId();
e.setAlarms(app.edo.getAlarmsById(id));
e.setSubtasks(app.edo.getSubtasksById(id));
e.setNotes(app.edo.getNotesById(id));
}
}
resultsIntent.putExtra(RETURN_EVENTS, (Serializable)liteEvents);
}
// assemble archived event objects and insert
if(rEventsArchived){
List<Event> liteEventsA = app.edo.getAllArchivedEvents();
resultsIntent.putExtra(RETURN_EVENTS_ARCHIVED, (Serializable)liteEventsA);
}
// collect the shared data and send it
if(rSharedPrefs){
SharedPreferences prefs = getSharedPreferences(getString(R.string.PREFS_FILE_NAME), MODE_WORLD_READABLE);
resultsIntent.putExtra(RETURN_SHARED_PREFS, (Serializable)(prefs == null? null : prefs.getAll()));
}
Log.i(SERVICE_NAME, "returning results");
// send everything to the caller
sendBroadcast(resultsIntent);
}
}
Regardless whether the class is a service or broadcast receiver, you must register it with the application as a "receiver" (meaning it can accept intent broadcasts of some kind); this can be done in Java code or in the Android manifest. An example I use for that service in the manifest is below (albeit my Java class doesn't care, about the intent action yet):
<!-- Notification Launcher -->
<receiver android:name="edu.clarkson.dtask.BK.NotificationReceiver" android:enabled="true">
<intent-filter>
<action android:name="edu.clarkson.dtask.BK.NotificationReceiver.DISPATCH_ALARM" />
<action android:name="edu.clarkson.dtask.BK.NotificationReceiver.CANCEL_ALARM" />
</intent-filter>
</receiver>
Different system actions and notifications will be broadcast by the OS when a certain state changes; if you would like to hear about these changes, you register a broadcastreceiver or intentservice the same way as above in the manifest for a different Intent action (such as ACTION_BATTERY_STATE_CHANGED). All the documentation can be found on the android dev site. I hope that will get you started on the right path.
精彩评论