Launch activity/intent from Android Wear without opening ui on phone - android

I'm building an android-app with wear-support and having the following problem when I add a notification with the voice-action specified here:
http://developer.android.com/training/wearables/notifications/voice-input.html
// Create an intent for the reply action
Intent replyIntent = new Intent(mainActivity, VoiceActivity.class);
replyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent replyPendingIntent =
PendingIntent.getActivity(mainActivity, 0, replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the reply action and add the remote input
NotificationCompat.Action voice_action =
new NotificationCompat.Action.Builder(R.drawable.ic_action_mic,
getString(R.string.label_voice), replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Everytime the wearable calls the voice action / pending intent(?) my activity starts in onCreate() and opens my MainActivity-UI on the phone! But the thing is, the user is working simultaneously on the phone and don't want to get interrupted by a simple voice-action.
Any ideas how to pretend the voice-command from opening the app on phone when it's "minimized"?

Related

Handle notification click in background

I have added two action button to the notification but I'm not able to handle the click event in background.
Intent ok = new Intent(mContext, MainActivity.class);
ok.putExtra("OK",true);
PendingIntent intentOK = PendingIntent.getActivity(mContext, 0 , ok, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.ic_ok, delivered(), intentOK);
mBuilder.addAction(R.drawable.ic_ko, notdelivered(), intentKO);
The actions needs an intent that force my app in foreground.
I need to post user choice to a remote server without popping up the app.

Launching an activity on micro app (android wearable)

I am creating notification from my app on Phone in the below way.
private void launchMicroAppFromWearableNotif() {
int notificationId = 001;
// The below action has been defined in the micro app
Intent i = new Intent("com.microapp.action.PLAY_MUSIC");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent playPendingIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.play_music)
.setContentTitle("Play Music")
.setContentText("Play Music on Wear")
.setContentIntent(playPendingIntent)
.addAction(R.drawable.play_music, getString(R.string.act1), playPendingIntent);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
}
I have created a wearable micro app which has an activity(PlayActivity) with action "com.microapp.action.PLAY_MUSIC" defined in wearable Manifest.
When I take the action from notification on the wearable I expect the activity form the micro app to be launched. But nothing happens. Could someone please help on this. Is it the right way to do it?
Actions that you add to a notification are for the device that the notification was created on, so if you create a notification on your phone, when you click on it on your watch, the action will be sent back to your phone to be executed.
If you want to open your activity on your watch, you cannot achieve that by sending a notification from your phone in the manner you have done. You have a couple of options: for example you can send a message from your phone to your watch and then have the wear side of your app catch that message and put out a "local" notification on the watch; then the actions on that notification are local to your watch and you can open your desired activity via the actions in that notification. If you directly want to open an activity on your watch from your phone (i.e. you don't need a notification to show up on your watch), then you can handle the message that I talked about on your watch and instead of creating a notification there, simply open the desired activity.You may find the WearCompanionLibrary useful in handling some of these cases.

Different Notifications for Smart watch and Phone

Im looking for a way to send 2 different notifications out from an app. On the phone the notification will display one notification but on the watch it needs 2 display 3 separate notifications. Is there a way to send these to the watch so that they don't display on the phone as well?
Thanks
You should be able to send separate notifications to the watch. Google walks through the process of how to add wearble features to notifications here:
http://developer.android.com/training/wearables/notifications/index.html
This page shows you how to send a wearable only notification:
http://developer.android.com/training/wearables/notifications/creating.html
In particular this section from the above site:
Specify Wearable-only Actions
If you want the actions available on the wearable to be different from
those on the handheld, then use WearableExtender.addAction(). Once you
add an action with this method, the wearable does not display any
other actions added with NotificationCompat.Builder.addAction(). That
is, only the actions added with WearableExtender.addAction() appear on
the wearable and they do not appear on the handheld.
// Create an intent for the reply action
Intent actionIntent = new Intent(this, ActionActivity.class);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_action,
getString(R.string.label), actionPendingIntent)
.build();
// Build the notification and add the action via WearableExtender
Notification notification =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_message)
.setContentTitle(getString(R.string.title))
.setContentText(getString(R.string.content))
.extend(new WearableExtender().addAction(action))
.build();

Why can't I start an Activity from a page Notification on Android Wear?

I just succesfully created a bunch of pages notifications on my Wear device.
The only problem is that the PendingIntent does not seems to start an Activity
(which is of course declared in Manifest).
Here is my code:
List extras = new ArrayList();
Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class);
viewIntent.putExtra("KEY", "TEST123");
//Note: I also tried: Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent viewPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, viewIntent, 0);
for (Route aRoute : myRoutes) {
Notification aNotif = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("BUS " + aRoute.route_short_name)
.setContentText(aRoute.directions.get(0).trip_headsign)
.setLargeIcon(bitmap)
.setContentIntent(viewPendingIntent)
.setSmallIcon(R.mipmap.ic_launcher).build();
extras.add(aNotif);
}
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(desc)
.setContentIntent(viewPendingIntent)//Just in case
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder1
.extend(new NotificationCompat.WearableExtender()
.addPages(extras))
.setContentIntent(viewPendingIntent)//Just in case
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notification);
When I press on a Notification, I expect the intent to start, but nothing happens..
Any suggestion is welcome.
EDIT:
This code works, just after the notification, so, the second activity can easily be launched withour bug:
startActivity(viewIntent);
EDIT2:
There is now an "open" button at the end that works fine, but still nothing happens on individual notifications (every pages)
Pages are not clickable - on Android Wear, only actions are clickable. For phone generated notifications, those only appear after all pages
If you have a content intent on your phone generated notification, that will always appear as an 'Open on phone' action. There is no way to disable this unless you remove your content intent (making the notification unclickable on phones).
I say 'phone generated' as you can also create a Wear app. By using the data layer to push messages to your Wear app, the Wear app can then build custom notifications. These notifications allow you to use setDisplayIntent() and display activities inline (either as the main page or as separate pages). These activities can, of course, contain any View you want, including actions to perform any action (such as send a message back to the phone to start a particular activity).
Note that because pages are not clickable by default, styling of a custom notification should make it very obvious that the items are clickable. Rather than using a custom notification activity, you may consider using setContentAction() to display the action icon inline with the rest of the layout - this removes the action as a separate element past the action and places it directly on the notification/page.

Android Remote View Notifications

I have an android notification bar which uses a remote view. I have 2 buttons on there to play and pause audio and also an icon to return to the application. I want to be able to click the icon (or anywhere but on the 2 buttons) and return to the application. Here is my code
Intent returnIntent = new Intent(_context, SplashScreenActivity.class);
returnIntent.setAction(Intent.ACTION_MAIN);
returnIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent remoteViewPendingIntent = PendingIntent.getActivity(_context,1, returnIntent,0);
remoteView.setOnClickPendingIntent(R.id.btnAppIcon, remoteViewPendingIntent);
This all works fine in the emulator on Android 4.1. When the icon in the notification is pressed it returns successfully to the app. However on Android 4.0.3 on a Samsung S3 the Activity launches in the background but the notification screen is not hidden. I want the notification screen to be cleared once the icon is selected. I have tried using the Notification.FLAG_AUTO_CANCEL in the notification but that did not fix the issue. Any help would be greatly appreciated.
.setOnClickPendingIntent behavior varies on different MFGs and versions as you've experienced.
To accomplish what you want and auto collapse the notification window automatically (without using reflection), you have to set a "global" pending intent for the notification view. You will need to include your R.id.btnAppIcon in that "catch all" intent view as well.
Example of the "global/default" notification pending intent:
contentIntent = PendingIntent.getActivity(YourClass.this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
The "general" notification intent when used with PendingIntent.getActivity will close the notification window on any device (as I've observed).

Categories

Resources