I have a litte appwidget with a configuration activity.
In the configuration activity I have a EditText
to store a particular string and a button to create the widget.
In the widget itself there is only a Imageview with a static image showing a button.
when i press the button im doing the following:
AppWidgetManager app_widget_manager = AppWidgetManager.getInstance(context);
RemoteViews local = new RemoteViews(context.getPackageName(), R.layout.main);
app_widget_manager.updateAppWidget(widget_id, local);
Intent create = new Intent(this, MyWidget.class);
create.setAction("com.test.mywidget.CREATE");
create.putExtra("toastmessage", edittext1.getText().toString);
context.sendBroadcast(create);
Intent result_value = new Intent();
result_value.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
setResult(RESULT_OK, result_value);
finish();
the extra 'toastmessage' is just the content of the edittext.
in the widget i catch the broadcast in onReceive() and get the string value that was
passed before as extra 'toastmessage' to the new widget.
if i press on my widget, i create a toast with the passed string
- and here is my problem -
the passed string is different for every widget created.
but the variable (in the widget) where i store the passed string
gets resettet after a little time or when the phone gets restartet or the app killed or something similar,
so i need persistent data for every widget instance.
I need to store which widget got which string passed,
and get this data everytime when the widget gets updated.
How can i achieve this?
According with this link you have some options to accomplish your goal.
For shared preferences, here an example.
For internal storage Take this way.
For external storage, you can use this.
For SQL Lite storage, here.
Hope these examples helps, cheers.
The widget_id you received is a unique identifier of the widget instance. Use it as a key to identify your persisted data.
Related
I tried to make a SceneView intent where a 3d-model can be viewed.
Intent sceneViewerIntent = new Intent(Intent.ACTION_VIEW);
sceneViewerIntent.setData(Uri.parse("https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Avocado/glTF/Avocado.gltf"));
sceneViewerIntent.setPackage("com.google.android.googlequicksearchbox");
sceneViewerIntent.putExtra("mode","3d_only");
mContext.startActivity(sceneViewerIntent);
When this intent opens, it gives the user an option to view the model in your own environment, but I want to turn this off and according to https://developers.google.com/ar/develop/java/scene-viewer#3d-or-ar you should be able to pass on a parameter called 'mode' which can have the value '3d_only' which should prevent the user from being able to view the model in AR.
I tried to pass on this value by means of sceneViewerIntent.putExtra("mode","3d_only"); but it doesn't work. Am I passing on the information correctly?
You're not sending any information anywhere. "3d_only" should be some variable, otherwise you're not sending anything. Also, you have to retrieve your code on the other end by doing String string = getIntent().getStringExtra("mode");
I am working with an android project for which I want to create a widget. Using this widget I have to control some actions of the control (that is by pressing the stop button from widget the app should stop working of some action). For that I have to pass some value to app from the widget on a button click. So far I am able to do the following:
open an activity on button click
open an URL on button click
make a toast on home screen on button click
Any help is appreciated,thank you.
I have successfully completed this part of my project.There are two scenarios
When we are passing values the app will be launched. That is we are passing the values directly to an activity
With out launching the app we can pass the values.Here we are using service
In both situation we can use two method
Using sharedpreferences
Using Intent(putExtra())
If you want pass the value directly to an activity(the App will be launched while passing the value) you can use second method above mentioned and call the activity(which activity you wanted to be launched) as follows:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.PunchIn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
this code to launch MainActivity from widget button click. In Inten you can use putExtra() to pass values.better to not use sharedpreference here.
For passing value with out launchingthe app,you can call service from widget button then you can add whatever code you want to add in the service.class file. you can call the service as follows:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent intent = new Intent(context,MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.PunchIn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
from this service class you can pass values to the application using SharedPrefernce or putExtra().
if you want to make a button widget which work same as button click of the app you can use second method. Here you can call a service on on button click as mentioned above, Then write code for the button click in the service class.
i am new to android,i worked with widget in my project for that researched a lot and it took lots of time. so thought about posting this which will help anyone like me. if you found any error/mistake please correct me.happy to correct my mistake.
Thank you
I'm not sure with this problem because I have not try widget yet.
When button click, send a boardcast message. And your app needs to run a service to receive the message. You can put your values in the message?
Wish this link may give you some ideas:
http://rxwen.blogspot.com/2012/10/communication-between-android-widget.html
There is a similar question:Android - Communications between a widget and its app
Good luck!
I have 2 activities: Activity_A and Activity_B. On Activity_A I have a TextView with the id "MyTextView". How can I get its text from Avtivity_B?
I have tried to do this in Activity_B main Java file:
txtv = (TextView) findViewById(R.id.MyTextView);
txtv.getText().toString()
But this didn't work.
Is there any way to this without Intents?
You should get the content on the Activity where you have it and then send data from activity A to activity B with the intent:
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("fromActivityA",textView.getText());
And then you have to use getExtra() method on activity B for getting the data you send from activity A.
Hope you can find this helpful!
The challenge is that when an activity is not visible, it has been paused, and is no longer available. Also the id used in findViewById may refer to an id in the other activities layout file. Using an intent to pass the data when moving between activities is a good solution, or if you really need access to that data from different activities you could store it in shared preferences, in a database, or as a static field on the activity class itself.
I am using Android Studio for a simple communication app and this definitly might be a stupid question but I couldn't find the answer to it yet.
I am simply starting a new intent on a button click. However, the very first time the user does this, he is asked what kind of app he wants to use. Therefore, there are at least two applications with the same intent filter, namely "ACTION_VIEW"
My Problem:
After the initial click the button always "reuses" his initial choice so the user does not have a decision anymore which app he wants to use for this intent.
Unfortunately, this is excactly what I want. The user should get the chance to select the app of his favor each time he clicks on the button.
The relevant part of code is as simple as follows:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
Intent.FLAG_UPDATE_CURRENT could probably help you.
https://developer.android.com/reference/android/app/PendingIntent.html
You can use an App Chooser to show the dialog for the user to choose which app to use each time:
[I]f the action to be performed could be handled by multiple apps and the user might prefer a different app each time—such as a "share" action, for which users might have several apps through which they might share an item—you should explicitly show a chooser dialog.... The chooser dialog forces the user to select which app to use for the action every time (the user cannot select a default app for the action).
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
I have an activity for handling deeplink which is a browsable activity
suppose user clicks a link on another app and my browsable activity handles that intent
and start the app, , then user minimise the app by pressing back button
class code for handling intent data
Uri link = getIntent().getData();
if user reopen app from running tasks getIntent() still have data
onDestroy method of browsable activity
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
setIntent(null);
}
setIntent(null) not working
so my question is how can i remove data from intent permanently
I am a little late on the answer here but I was dealing with a similar issue and found a solution. The reason you are still seeing data in the intent is because your app was originally started with an intent that contained data. That original intent is stored somewhere in Androids' ActivityManager and is immutable, to my understanding. When user reopens the app from "recent tasks", Android uses that original intent to recreate the application.
There is a workaround to this, however. In your apps onCreate() method, you can check to see if the Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY is set, which would allow your app to distinguish if it is being started from "recent tasks" or not, and therefore, you can avoid using the data contained in the intent.
Putting the following snippet in your onCreate() method would return true if the app is being opened from "recent tasks"
(getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0)
you have to remove data one by one key. i think you can't remove all data with one line.
if you want to remove specific key than you should use ==> getIntent().removeExtra("key"); or
getIntent().setAction("");
it will remove your data.
for more ==> Clearing intent
To remove the content it works best for me with the following lines of code.
//Clear DATA intent
intent.setData(null);
intent.replaceExtras(new Bundle());
intent.setFlags(0);
After that they can verify that the intent does not contain data