I have the following code setup to launch the voice recognizer with a pendingintent to launch another activity:
Intent voiceActivityIntent = new Intent (MainActivity.this, VoiceActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity (MainActivity.this, 0,
voiceActivityIntent, PendingIntent.FLAG_ONE_SHOT);
Intent intent = new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
.putExtra (RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
.putExtra (RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pendingIntent);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The documentation states that startActivity can't be used with RecognizerIntent.ACTION_RECOGNIZE_SPEECH, and using startActivityForResult simply returns the result to the current activity (MainActivity) which is not desired.
I've tried:
pendingIntent.send ();
but this simply take me to VoiceActivity.class without executing the recognizer.
I'm currently testing on the Android Wear Round API 21 emulator.
just add the following to your code:
// this intent wraps voice recognition intent
PendingIntent pendingIntVoice = PendingIntent.getActivity(context, 0, intent, 0);
pendingIntVoice.send();
In other words, with pendingIntent.send (); you're not calling the Voice Recognizer Intent, BUT the Intent that should be automatically called when voice recognition ends (infact you set it with putExtra (RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pendingIntent) line)
I wanted something similar, here's what I came up with and it works. This is inside a Service:
//IntentHandlerService extends IntentService
//The intent I want sent back to me after voice recognition is complete...
Intent inputTextIntent = new Intent(this, IntentHandlerService.class);
//...gets wrapped in this PendingIntent...
PendingIntent pendingIntent = PendingIntent.getService(this, 0, inputTextIntent, PendingIntent.FLAG_ONE_SHOT);
//...and added to the intent aimed for the recognizer...
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pendingIntent);
//...which is the one you start.
startActivity(intent);
Related
Is it possible to have a Toast being shown when I click on one of the Variants of Notification? I tried to sending this Toast by intent to another class but could not find a way to do so.
Also, i have tried just creating intent to another class which just has a function of creating a Toast message upon being called, but this did not work because the Toast was being shown upon creation of Notification. Please help. Thanks.
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("Info")
.setContentTitle("Info ")
.setContentText("NotiText")
.addAction(R.drawable.ic_stat_name, "This should make Toast upon clicking", pendingIntentCall)
.setSmallIcon(R.drawable.ic_stat_name).setAutoCancel(true);
Your intent does nothing. When passing the Intent to PendingActivity.getActivity(...), you must define it as an explicit intent for an Activity. For example:
Intent intent = new Intent(this, SomeActivity.class);
But if you only want to show a toast message, it would be probably better not to start an activity, because that would be quite an expensive operation for that purpose. Instead, you can create a PendingIntent also for a service or a broadcast. For example:
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
And MyBroadcastReceiver.java could look like this (basically):
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context.getApplicationContext(), "Your message", Toast.LENGTH_SHORT).show();
}
}
Btw, it is recommended to add an action or extra to the intent when you create it, and check for that action or extra when receiving the intent to verify that it's actually the intent that you expected to receive.
So I have a service, in the onCreate() I setup 3 pending intents to start the same service, each having different extra data to specify the action. I'm creating a notification, and I want to use one pending intent as the click action, one as the dismiss action, and the third is for an alarm.
Intent iCancel = new Intent(this, BootService.class);
Intent iAlarm = new Intent(this, BootService.class);
Intent iDismiss = new Intent(this, BootService.class);
// each will have actions
iAlarm.putExtra(INTENT_ACTION, INTENT_ALARM_FIRED);
iCancel.putExtra(INTENT_ACTION, INTENT_CANCEL);
iDismiss.putExtra(INTENT_ACTION, INTENT_DISMISS);
PendingIntent piCancel = PendingIntent.getService(
this,
0,
iCancel,
Intent.FILL_IN_DATA);
mPiAlarm = PendingIntent.getService(this, 0, iAlarm, Intent.FILL_IN_DATA);
PendingIntent piDismiss = PendingIntent.getService(this, 0, iDismiss, Intent.FILL_IN_DATA);
mNotifyBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_about)
.setContentTitle(getString(R.string.app_name))
.setContentIntent(piCancel)
.setDeleteIntent(piDismiss);
The problem is all pending intents seem that have the same intent extra data, so when onStartCommand is launched no matter whether the notification was clicked or dismissed or neither, constant INTENT_CANCEL is received from intent.getIntExtra(INTENT_ACTION)
I believe it has something to do with the flags used in PendingIntent.getService(), i'm confused about which to use. I've tried using PendingIntent.FLAG_CANCEL_CURRENT, and UPDATE_CURRENT, neither seem to fix the issue but the result is different, I receive constant INTENT_ALARM_FIRED for every action.
How can I get each pending intent to have its own intent extra data?
Solution
I discovered an exact warning about this scenario right in the PendingIntent doc. http://developer.android.com/reference/android/app/PendingIntent.html
just changed my request codes and it works
This has to do with Intents being considered the same. See PendingIntent for more information.
One way to get around this would be to just vary the requestCode for each Intent. (The 2nd parameter in the getService call)
is it possible to make an Intent whic runs in background? Like a service i assume.
I need to perform an action (call webservice) when the user clicks on a widget.
My code below
Intent intent = new Intent();
intent.putExtra("webServiceId", mWSId);
PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
Should I make my intent to perform an action to a service or something like that?
Thanks!
UPDATE:
Im trying to do it by calling an IntentService now, but it doesnt calls it. My code below:
Intent intent = new Intent(context, widgetService.class);
intent.putExtra("webServiceId",mWSId);
PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
any idea?
I am not sure what you are trying to achieve
I am right you want to call a widget on click of a widget(view)
This can be achieved in a simple way by calling the onClick action
of a widget and place the web-service invocation code inside the
onCllickListener
Sample
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Place your webservice invocation code here
}}
If you want to use service in some way have a look at android documentation on how to use it
Hope this helps !
I solved it by calling a IntentService.
Here is my code:
Intent intent = new Intent(context, widgetService.class);
intent.putExtra("webServiceId",mWSId);
PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
Thanks #Karakuri
Context.startService
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
PendingIntent.getService
Intent intent = new Intent(context, MyService.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
pi.send();
Questions
When would you start a service with Context.startService vs a PendingIntent?
Why would you use one over the other?
There really is no difference.
Specifically the Context method is used to directly start it where as a PendingIntent is typically used with a notification to fire this intent when it is tapped, which is delayed until the user taps it (typically). However; you wouldn't typically send the PendingIntent directly because that is not what it is for.
A PendingIntent is an Intent that is pending, pending, meaning that its NOT supposed to happen now, but in the near future. Whereas with an Intent, it is sent at the very moment.
If a PendingIntent is not pending when it is used, then it is no longer a PendingIntent and it is infact an Intent. Defeating the purpose entirely.
PendinIntents are very much used for widgets. As the layout of a running widget doesn't "belong" to your code, but it is instead under control of the system, you can't assign directly click listeners to the interface elements. Instead you assign a PendingIntent to those elements (like buttons) so when the user touches them, the PendingIntent is "executed", something like:
// get the widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.id.widget_layout);
// prepare to listen the clicks on the refresh button
Intent active = new Intent(context, WidgetCode.UpdateService.class);
PendingIntent refreshPendingIntent = PendingIntent.getService(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.buttonWidgetRefresh, refreshPendingIntent);
// send the changes to the widget
AppWidgetManager.getInstance(context).updateAppWidget(appwidgetid, remoteViews);
In this case a button in the widget starts a service. Usually you put extra info in the intent, with putExtras(), so the service will get any needed information to do its job.
I want my app to show a login-screen every time it is opened. With android:clearTaskOnLaunch="true" everything works fine. But there is a bug which is happening only if the phone was shutdown and the app was first started with the widget.
Intent intent = new Intent(context, StartActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
[....]
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_onebyone)
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
This code should start my mainactivity. At first start is does. But ending the app with HOME and restart it with the widget causes my app to not start with my startactivity but with another activity of my app without showing the loginscreen.
What can I do to fix it.
I've tried
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
But it does not work. All I want is to (re)-start the StartActivity-Intent like in startActivity(..) but I found no option to give my Widget an OnClickListener which starts a "normal" Activity.
Best regards,
Till
Have you tried adding the Flag to the intent:
Intent intent = new Intent(context, StartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
[....]
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_onebyone)
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
Intent API
setFlags(int)
FLAG_ACTIVITY_CLEAR_TASK