Send String from Appwidget to New Activity - android

I'm working on an appWidget which has a textView, all i want is preview this textView in a new activity that i launch with
Intent configIntent = new Intent(context, ViewFromWidget.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent myPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widgetLayout, myPendingIntent);
Can some one point me out on how can i send a string to the new activity that u start via appWidget?

That should be easy, you want to use an intent extra for passing strings.
Add this before setAction
configIntent.putExtra("MyKey", myString);
And in the activity getting the intent use getStringExtra
String passedString = getIntent().getStringExtra("MyKey");

Related

How to add parameters into PendingIntent

I am very new to Android Programming. I would like to know if it is possible to add extra parameters using PendingIntent ?
For example I wanna do something like intent.putExtra("test", key) in PendingIntent. Is it possible ?
You can add parameters in intent.
Intent intent= new Intent(context, YourActivity.class);
String displayName = "abc"
intent.putExtra("name", displayName);
PendingIntent pendIt = PendingIntent.getActivity(context,
TxrjConstant.REQUEST_READ_NOTIFICATION,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

Open Activity with setPendingIntentTemplate

I'm doing a widget with one stackView which i'm adding bitmaps(images) inside with a RemoteViewFactory.
My problem is when I want to do a onClickPendingIntent() for each image (that I have inside the stackView). I know I have to use setPendingIntentTemplate() instead, but I don't know how to go to an Activity when I press some widget's image.
Then this is what I have inside onUpdate() from widget class to create the Pending Intent Template:
Intent templateIntent = new Intent(Intent.ACTION_VIEW);
templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent templatePendingIntent = PendingIntent.getActivity(
context, 0, templateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.widget_stack_view,
templatePendingIntent);
And this is what I have in a RemoteViewsFactory class to handle the Pending Intent Template:
public RemoteViews getViewAt(int index){
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.grid_item_layout);
rv.setImageViewBitmap(R.id.image, images.get(index).getImage());
Intent fillInIntent = new Intent();
fillInIntent.putExtra(Intent.EXTRA_TEXT, images.get(index).getTitle());
rv.setOnClickFillInIntent(R.id.image, fillInIntent);
return rv;
}
So far, this fillInIntent.putExtra(Intent.EXTRA_TEXT, images.get(index).getTitle()); is acting with the phone showing me a "use complete actions" where you can choose with which app you want to open, but if you see what i'm putting in the putExtra() is a String. But what I want is open a Activity and then pass with putExtra() a image (images.get(index).getImage()) that is a bitmap
I figured out what was wrong:
Intent templateIntent = new Intent(context, Activity_I_want_to_open.class);
templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent templatePendingIntent = PendingIntent.getActivity(
context, 0, templateIntent, 0);
Then, I didn't need to FLAG_UPDATE_CURRENT, because I want to updating nothing.
And in the RemoteViews side:
Intent fillInIntent = new Intent();
rv.setOnClickFillInIntent(R.id.image, fillInIntent);
I didn't need putExtra() because I'm not sending data to other activity.

Working example of Recognizer PendingIntent flow

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);

Save to DB after clear the push notification android

I am working with push notification and I have one problem.
When I click in the received notification my code launch a new intent with an activity.
I use:
Intent i = new Intent(getApplicationContext(), DemoActivity.class);
i.putExtra("msg",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
ITS OK FOR ME
But if I want only save this notification in SQLite how can I pass the message?
I know that with:
Intent bd = new Intent(getApplicationContext(), AbaseDatos.class);
bd.putExtra("msg",msg);
PendingIntent contentIntentbd = PendingIntent.getActivity(this, 0,
bd, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setDeleteIntent(contentIntentbd);
Call a new activity when clear the notification, but the new activity open the layout.
I dont want any new activity be launched. Its possible launch a javaclass with an Intent?
Any idea?
You can launch a service with an intent.
http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context%2C%20int%2C%20android.content.Intent%2C%20int)

How can I check which widget was clicked?

I need to show different data when the different widget will be clicked. For example widget one will show me activity with number 1 and widget to with numer 2 :)
Perhaps I can send some data by intent from widget, but then how to recive that data in activity class?
Widget class
Intent intent = new Intent(context, Information.class);
intent.putExtra("widget_id", appWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget1x1);
remoteViews.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
Bundle extras = intent.getBundle();
String id = extras.getString("widget_id");

Categories

Resources