Lancement d'un fragment dans mon application Android à partir de la barre de notification
Comment puis-je commencer un fragment dans mon application Android à partir d'une notification dans la barre de notification?
J'ai essayé de mettre en œuvre cette réponse de créer ma propre action, puis en définissant l'action de l'esprit, mais je n'en suis pas sûr comment l'utiliser et ce qui est exigé en outre - comme l'ajout de quelque chose pour le Manifeste.
J'ai une classe de notification qui reçoit un contexte, un message et une action. Je veux filtre sur cette action pour déterminer le fragment de lancement, mais je ne sais pas comment lancer un fragment plutôt que de lancer une activité.
Voici mon Notifications.java catégorie (incomplet):
public class Notifications {
private Context mContext;
public Notifications(Context context) {
this.mContext = context;
}
public static void notify(Context context, String message, String action) {
//Action you invent should include the application package as a prefix — for example: "com.example.project.SHOW_COLOR".
action = "my.package.name.here.frag."+action;
//Construct a user message.
String appName = context.getResources().getString(R.string.app_name);
//Use the Notification manager to send notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//Create a notification using android stat_notify_chat icon.
Notification notification = new Notification(R.drawable.ic_stat_notification, message, 0);
//Sound, lights, vibration.
//REMEMBER PERMISSIONS.
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
//Create a pending intent to open the application when the notification is clicked.
//Restart the app.
Intent launchIntent = null;
//Get the action and based on what the action is, launch the application displaying the appropriate fragment.
if (action.equalsIgnoreCase("friend")){
//New friend notification
//Launch application displaying the list of friends
}
if (action.equalsIgnoreCase("article")){
//New article has been posted
//Launch application displaying the news feed fragment
}
if (action.equalsIgnoreCase("points")){
//Points scored notification
//Launch application displaying the user's profile
}
if (action.equalsIgnoreCase("redeemable")){
//New redeemable is offered
//Launch application displaying the list of redeemables
}
if (!action.equalsIgnoreCase("friend")
&& !action.equalsIgnoreCase("article")
&& !action.equalsIgnoreCase("points")
&& !action.equalsIgnoreCase("redeemable")){
//Not specific, so launch the application from scratch displaying the activity feed
launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
}
if(action != null && launchIntent != null){
launchIntent.setAction(action);
}
//Set the notification and register the pending intent to it
notification.setLatestEventInfo(context, appName, message, pendingIntent);
//Trigger the notification
notificationManager.notify(0, notification);
}
}
OriginalL'auteur marienke | 2013-08-22
Vous devez vous connecter pour publier un commentaire.
Donc c'était assez facile. J'espère que je peux aider quelqu'un d'autre voir ce trop.
J'envoie une action à ce
notify
fonction. L'-je ajouter que l'action de monintent
pour lancer une activité. Dans mon cas, j'ai ouvert le lancement de l'activité, parce que tous les fragments sont chargés de l'intérieur de cette activité en fonction de ce que fait l'utilisateur. J'ai donc définir l'action à l'aide desetAction
et j'utilise que des actions dans l'activité ci-dessous.Mon
Notifications.java
classe changé:Et puis dans mon
activity
de l'endroit où je charge les fragments, je reçois l'action et le filtre:Dans mon
goFrag
fonction que j'ai remplacer le fragment si le fragment est encore dans la mémoire (ce qui signifie que l'utilisateur a été là plus tôt, et il n'a pas été détruit encore), ou je créer un nouveau fragment nécessaire.String[] frag_tags = { "fragment1", "fragment2", "fragment3"}; Fragment de frag = getSupportFragmentManager().findFragmentByTag(frag_tags[fragNumber]); if (frag != null) { Log.d(TAG, "Frag est dans la mémoire:" + String.valueOf(fragNumber)); de la transaction.remplacer(R. id.lin_main_container, frag, frag_tags[fragNumber]); } else { Journal.d(TAG, "Recréer frag:" + String.valueOf(fragNumber)); }
pourquoi mon action est nulle?
est la deuxième partie du code écrit dans
OnCreate
ouNewIntent
? Comment prendre soin de que lorsque l'activité est déjà en cours d'exécution ou lorsqu'il ne l'est pas.pensez à utiliser les balises de votre fragments il est bien plus simple
OriginalL'auteur marienke