How to save and retrieve an android widget id - android

I have setup a widget like this
ToggleWidget.java
public class ToggleWidget extends AppWidgetProvider {
static RemoteViews remoteViews;
boolean status = false;
static int appWidgetId;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
appWidgetId = appWidgetIds[i];
remoteViews = new RemoteViews(context.getPackageName(), R.layout.toggle_widget);
Intent intent = new Intent(context.getApplicationContext(), WakeService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.bulb_widget, pendingIntent);
if (WakeService.isAwake) {
remoteViews.setImageViewResource(R.id.bulb_widget, R.drawable.bulb_on);
}
else {
remoteViews.setImageViewResource(R.id.bulb_widget, R.drawable.bulb_off);
}
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}
}
How can i save the ID of every widget i create and delete those that i've deleted with sharedPreferences?
Also from a service i'd like to update the widget like this
private void updateWidget(){
Intent intent = new Intent(this,ToggleWidget.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
int[] ids = {ToggleWidget.appWidgetId};
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);
}
How can i retrieve the saved IDs from the service to make the update?

In your Reciever class just override onCreate() method and under that method,
int[] ids = getIntent().getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
Then you will get int array, pls check it, it will work...:)

Take a look at android's Data Storage options. Here is how you could use SharedPreferences...
public class MyService extends Service{
//Class used to store data
private SharedPreferences preferences;
private int[] ids;
private int numIds;
public void retreiveIds(){
preferences = PreferenceManager.getDefaultSharedPreferences(this);
numIds = preferences.getInt("num",0);
ids = new int[numIds];
for(int i=0;i<numIds;i++){
ids[i] = preferences.getInt("ID"+i,0);
}
}
public void saveIds(){
SharedPreferences.Editor ed = preferences.edit();
numIds = ids.length;
//iterate through array and save each id
for(int i=0;i<numIds;i++){
ed.putInt("ID"+i,ids[i]);
}
ed.putInt("num",numIds);
ed.commit();
}
}

There is another way to do it. Code in kotlin:
val thisWidget = ComponentName(context, this.javaClass)
val allWidgetIds: IntArray = appWidgetManager.getAppWidgetIds(thisWidget)
Now you can use these allWidgetIds to update widgets.

Related

android widget as async task

sorry for question which was many time answered but for me is still now working :-(
I have following activiy code:
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int count = appWidgetIds.length;
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.simple_widget);
Intent intent = new Intent(context, SimpleWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
new Weather(remoteViews).execute();
remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
with Async Weather class but this doesn't work. Value is missing in remoteViews.
class Weather extends AsyncTask<Void, String, String> {
private RemoteViews remoteViews ;
public Weather(RemoteViews remoteViews) {
this.remoteViews = remoteViews;
}
#Override
protected String doInBackground(Void... arg0) {
return "COLD";
}
#Override
protected void onPostExecute(String temp) {
remoteViews.setTextViewText(R.id.textView, temp);
}
Whatever RemoteViews that you pass to updateAppWidget() will be used at that time. In your case, your Weather task will not even begin to be executed by the time that the RemoteViews has been used, and so your change to that RemoteViews will be pointless.
Instead:
Switch to Thread, as there is no value in using AsyncTask here, and
Do all the work for a given widget ID, including calling updateAppWidget(), in the Thread

How to update android Widget with a string array

I'm a newbie and i'm finding difficulty in getting texts from a string array. I tried creating a widget in my app and i wanted the widget to update it's text view from the string array i declared in the resources folder, but this does not seams to work out. Here is my code.
MainActivity.Java
public class MainActivity extends AppCompatActivity {
public static TextView Quote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Quote = (TextView) findViewById(R.id.appwidget_text);
final String[] Quotes = getResources().getStringArray(R.array.Qoutes);
Random rand = new Random();
int randNum = rand.nextInt(Quotes.length);
Quote.setText(Quotes[randNum]);
}}
NewAppWidget.java
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = MainActivity.Quote.getText().toString();
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0 , intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
remoteViews.setOnClickPendingIntent(R.id.interfaceID,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}}
Thank You

How to keep widget configuration after phone restart?

I want to make a widget app that is able to dial a number and user can set the number when he first drag and drop widget to home screen using widget configuration.But when the phone restarts widget uses default number again. I decide to save the entered phonenumber to Shared Preferences to save and load user's phone number but eclipse says that using getSharedPreferences is not allowed in onUpdate.Is there another way to perform it?
What should I do?
my code:
public class Main extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
for(int i=0 ; i<appWidgetIds.length ; i++)
{
SharedPreferences details = getSharedPreferences("OPERATOR", 0);
int appWidgetId = appWidgetIds[i];
String phNumber = "5554574";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+(phNumber)));
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setOnClickPendingIntent(R.id.button1, pending);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Use:
SharedPreferences details = context.getSharedPreferences("OPERATOR", 0);
SharedPreferences has to be acquired from the context. onUpdate(...) provides you the context.
Your changed code should look like this:
public class Main extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
for(int i=0 ; i<appWidgetIds.length ; i++)
{
SharedPreferences details = context.getSharedPreferences("OPERATOR", 0);
int appWidgetId = appWidgetIds[i];
String phNumber = "5554574";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+(phNumber)));
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setOnClickPendingIntent(R.id.button1, pending);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Please try it.
AppWidgetProvider is not a Context. But you can often create a static method to reach the context of an application, then do this
In Android Manifest file declare following
<application android:name="com.example.MyApplication">
</application>
then write the class
public class MyApplication extends Application{
private static Context context;
public void onCreate(){
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApplication.context;
}
}
Then you can use
SharedPreferences details = MyApplication.getAppContext().getSharedPreferences("OPERATOR", 0);
I haven't worked with widgets yet so can't guarantee this solution but hope it work!

How to get appWidgetId in AppWidgetProvider?

I need to get the specific appWidgetId in AppWidgetProvider, not a bundle of appWidgetIds[] but the specific id which user clicked.
I could get it in its configure activity, using the "intent.getExtra().getInt(AppWidgetManager.APP_WIDGET_ID)" says like 504.
But it is so different in AppWidgetProvider, I will be appreciate it if anyone give me a hand.thanks in advance.
public class WidgetBox extends AppWidgetProvider implements Widget {
private int _widgetId = ?; // not appWidgetIds[]
}
You will get an array of Ids when the widget life cycle methods are called.
You can simply iterate through the provided Ids like this:
#Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
final int n = appWidgetIds.length;
for( int i = 0; i < n; i++ ) {
final int appWidgetId = appWidgetIds[i];
// some logic here
}
}
if you need to invoke some action, this is done using a pending intent during configuration (or a later update):
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
final Intent defineIntent = new Intent(context, YourTargetActivity.class);
defineIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* no request code */, defineIntent, 0);
views.setOnClickPendingIntent(R.id.view_id, pendingIntent);

Getting a String to AppWidget via GetExtras (or) Shared prefrences

I have an app widged which basically has a textview. I also have
an activity which displays a textview from string created within. Now
i have a button in Activity 1 which onClick will send data (string to
appWidget class) I tried putExtra and getExtra methods and also Shared
preferences method, im little confused to use which first!
Here are some inputs from me for more clarity.
Activity1:
final String addwidget =
((TextView)findViewById(R.id.verse)).getText().toString();
Intent widgetIntent = new Intent(MyScheduledActivity.this,
MainWidget.class);
widgetIntent.putExtra("widgetVerse", addwidget);
AppWidget:
public class MainWidget extends AppWidgetProvider {
RemoteViews views;
public static String verseFromFav;
private Bundle getVerseData;
#Override
public void onReceive(Context context, Intent intent) {
getVerseData = intent.getExtras();
verseFromFav = getVerseData.getString("widgetVerse");
super.onReceive(context, intent);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
views = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
if(verseFromFav == null){
verseFromFav = "no verse";
}
views.setTextViewText(R.id.widgetVerse, verseFromFav);
ComponentName thisWidget = new ComponentName( context, MainWidget.class );
AppWidgetManager.getInstance( context ).updateAppWidget( thisWidget, views );
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
(THE APP WIDGET SHOWS "no verse" WHICH READS NULL)
Can any one help me with a basic idea on how to display the string
here. please. thanks in advance.
onUpdate will ALWAYS be called (sometimes multiple times) so utilize that to fire a utility or controller class and do all the work in that class.
Thus, in your app widget the only thing you might have, all I am demonstrating is updating a string in a TextView, is stuff in the onUpdate() method.
#Override
public void onUpdate( final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds ) {
// Need this here to set the initial state of the app widget
final AppWidgetController _actrlr = new AppWidgetController( context );
_actrlr.updateAppWidgetUI( this );
super.onUpdate( context, appWidgetManager, appWidgetIds );
}
Then in AppWidgetController class you might have something like this...
public final void updateAppWidgetUI( final Context context ) {
final AppWidgetManager _manager = AppWidgetManager.getInstance( context );
final ComponentName _componentName = new ComponentName( context, com.your.namespace.theprovidername.class );
final int[] appWidgetIds = _manager.getAppWidgetIds( _componentName );
for ( final int i : appWidgetIds ) {
final RemoteViews _rv = new RemoteViews( context.getPackageName(), R.layout.appwidget_xml_layout_file );
if( somethingIsTrue ) {
_rv.setTextViewText( R.id.textViewThatYouWantToUpdate, "The text that you wish to display if something is TRUE" );
} else {
_rv.setTextViewText( R.id.textViewThatYouWantToUpdate, "The text that you wish to display if something is FALSE" );
}
_manager.updateAppWidget( i, _rv );
}
}
RemoteView setTextViewText()
http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText(int, java.lang.CharSequence)
I would advise you to use sharedpreference for this. It would do you better.
SharedPreferences settings =
getSharedPreferences("MyWigdetPreferences", MODE_WORLD_READABLE);
SharedPreferences.Editor prefEditor = Settings.edit();
prefEditor.putString("widgetVerse", addwidget);
prefEditor.commit();
Then in the mainWidget activity pull it out by..
SharedPreferences settings =
getSharedPreferences("MyWigdetPreferences", MODE_WORLD_READABLE);
String verse = settings.getString("widgetVerse");
That should do it.
EDIT:
Instead of
#Override
public void onReceive(Context context, Intent intent) {
getVerseData = intent.getExtras();
verseFromFav = getVerseData.getString("widgetVerse");
super.onReceive(context, intent);
}
Try
#Override
public void onReceive(Context context, Intent intent) {
getVerseData = getIntent.getExtras();
verseFromFav = getVerseData.getString("widgetVerse");
super.onReceive(context, intent);
}

Categories

Resources