Programmatically bypass "touch to configure" Android Widget - java

I would like to know how to bypass the "Touch to Configure" on my android widget programmatically. Once I have installed the android widget on my device it says "Touch to configure" which then executes my class "IntelligenWidgetConfig.java". But I want this to automatically happen on start up of my application. Can anyone advise me on how to approach this method or any solution that might help?
Android Config Class:
public class IntelliGenWidgetConfig extends Activity {
private static final String TAG = "IntelliGenWidgetConfig";
public IntelliGenWidgetConfig() {
super();
}
public static int getAppWidgetId(Context context) {
SharedPreferences settings = context.getSharedPreferences("Intelligen", MODE_PRIVATE);
return settings.getInt("appWidgetId", 1);
}
protected void setAppWidgetId(int id) {
SharedPreferences settings = getSharedPreferences("Intelligen", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("appWidgetId", id);
editor.commit();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the result to CANCELED. This will cause the widget host to cancel
// out of the widget placement if they press the back button.
setResult(RESULT_CANCELED);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
setAppWidgetId(extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID));
}
// If they gave us an intent without the widget id, just bail.
if (getAppWidgetId(this) == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
final Context context = IntelliGenWidgetConfig.this;
RegisterForPushNotification(context);
// Push widget update
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.intelligen_widget_layout);
appWidgetManager.updateAppWidget(getAppWidgetId(this), views);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, getAppWidgetId(this));
setResult(RESULT_OK, resultValue);
finish();
}
private void RegisterForPushNotification(Context ctx) {
Intent registerIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registerIntent.putExtra("app", PendingIntent.getBroadcast(ctx, 0, new Intent(), 0));
registerIntent.putExtra("sender", "209845001369");
startService(registerIntent);
}
private void DeregisterFromPushNotification(Context ctx) {
Intent unregisterIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregisterIntent.putExtra("app", PendingIntent.getBroadcast(ctx, 0, new Intent(), 0));
startService(unregisterIntent);
}
}
AndroidManifest.xml (section):
<activity android:name=".IntelliGenWidgetConfig" android:label="#string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
XML
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="180dp"
android:minHeight="360dp"
android:updatePeriodMillis="0"
android:initialLayout="#layout/intelligen_widget_layout"
android:resizeMode="vertical"
android:minResizeWidth="280dp"
android:minResizeHeight="70dp"
android:configure="za.co.infotech.www.intelligen.IntelliGenWidgetConfig"
android:previewImage="#drawable/preview">
</appwidget-provider>

Related

Send data from BroadcastReceiver to Activity in background(without starting)

I have this receiver class, it' registered in my MainActivity, it's supposed to count unlocks.
My goal
I want to make it work even when the app itself is not running(the receiver is never unregistered). It should get the amount of unlocks to the main activity without starting/restarting the activity.
My problem
I have no idea how to do that. As you can see I have tried to do it with SharedPreferences, but it didn't work, the receiver works fine and gets broadcasts, I just don't want it to start everytime.
public class UnlockReceiver extends BroadcastReceiver {
private int count = 0;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private static final String TAG = "UnlockReceiver";
#Override
public void onReceive(Context context, Intent intent) {
preferences = context.getSharedPreferences("label", 0);
count = preferences.getInt("tag", 0);
KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
if (!keyguardManager.inKeyguardRestrictedInputMode()) {
count++;
editor = preferences.edit();
editor.putInt("tag", count).commit();
Log.d(TAG, "unlock registered");
Intent i = new Intent(context, MainActivity.class);
i.putExtra("tag", count);
context.sendBroadcast(i);
}
} else {
if (!keyguardManager.isDeviceLocked()) {
count++;
editor = preferences.edit();
editor.putInt("tag", count).commit();
Log.d(TAG, "unlock registered");
Intent i = new Intent(context, MainActivity.class);
i.putExtra("tag", count);
context.sendBroadcast(i);
}
}
}
}

ImageButton isnt working on Custom Notification - Android

I'm trying to create a custom notification with ImageButton.
When onclicked, the ImageButton should start an activity named TaskActivity, but i'm unable to achieve this.
The followings are my codes:
MainActivity.java
public void onCreate(Bundle savedInstanceState) {
//button intents
Intent cmdIntent = new Intent(
MainActivity.this, cmdButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, cmdIntent, 0);
//notification mgr
int notifyID = 001;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotifyMgr = (NotificationManager)getSystemService(ns);
//new notification
int icon = R.drawable.icon3;
long when = System.currentTimeMillis();
#SuppressWarnings({ "deprecation" })
Notification notify = new Notification(icon,getString(R.string.text),when);
//remote views
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.custom_notification);
contentView.setImageViewResource(R.id.notification_image, R.drawable.icon4);
contentView.setTextViewText(R.id.notification_title, "APP TITLE");
contentView.setTextViewText(R.id.notification_text, "hello");
contentView.setOnClickPendingIntent(R.id.notification_image, pendingSwitchIntent);
notify.contentView = contentView; //set
//notification intent
Intent nIntent = new Intent(MainActivity.this,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, nIntent, 0);
notify.contentIntent = contentIntent;
mNotifyMgr.notify(notifyID,notify);
}
public class cmdButtonListener extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
System.out.println("Here, I am here");
Intent newAct = new Intent(MainActivity.this, TaskActivity.class);
startActivity(newAct);
}
}
Android Manifest
<receiver android:name=".MainActivity$cmdButtonListener" />
I dont know, where i'm wrong as i'm doing exactly as it is shown in most of the tutorials online. :(
Finally I've found a solution...
Declare the below variable
private static Context mContext;
Add below code inside public void onCreate(Bundle savedInstanceState) {
mContext = this;
Finally, change the below piece of code in public class cmdButtonListener extends BroadcastReceiver{
Intent newAct = new Intent(MainActivity.this, TaskActivity.class);
startActivity(newAct);
To
Intent Act = new Intent(mContext,TaskActivity.class);
mContext.startActivity(Act);

How to update App Widget with list view from an Activity

I know this has been asked for many times but I went through the documentation from top to bottom, read all answers here and none of them helped.
To be honest, each answer says something different about how to aproach this.
Now back to my question. I want to update the widget list view from some activity and I created WidgetProvider#sendUpdateBroadcastToAllWidgets() for this purpose which I call from the activity.
It eventually calls the onUpdate() so the broadcast is received correctly. But the views are not refreshed.
I also tried to call AppWidgetManager#notifyAppWidgetViewDataChanged() and refreshed the data in WidgetFactory#onDataSetChanged() but the method has never been called.
So I guess this all does not work because the remote views factory is cached but I don't know how to reliably overcome this. Any thoughts?
And what about contexts? I always have to supply one but I don't really care much which one. Does it matter?
Thanks
PROVIDER
public class WidgetProvider extends AppWidgetProvider {
public static void sendUpdateBroadcastToAllWidgets(Context context) {
int allWidgetIds[] = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WidgetProvider.class));
Intent intent = new Intent(context, WidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.sendBroadcast(intent);
}
#Override
public void onUpdate(Context context, AppWidgetManager widgetManager, int[] widgetIds) {
for (int id : widgetIds) {
updateWidget(context, widgetManager, id);
}
super.onUpdate(context, widgetManager, widgetIds);
}
#Override
public void onDeleted(Context context, int[] widgetIds) {
WidgetPreferences prefs = new WidgetPreferences(context);
for (int widgetId : widgetIds) {
prefs.getWidgetPreferences(widgetId).edit().clear().commit();
}
super.onDeleted(context, widgetIds);
}
private static void updateWidget(Context context, AppWidgetManager widgetManager, int widgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
// set list adapter
Intent serviceIntent = new Intent(context, WidgetService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(android.R.id.list, serviceIntent);
views.setEmptyView(android.R.id.list, android.R.id.empty);
// set widget title
WidgetDataCategory category = new WidgetPreferences(context).getSavedCategory(widgetId);
views.setTextViewText(R.id.titleText, context.getString(category.titleResourceId()));
// set onclick listener - we create a pending intent template and when an items is clicked
// the intent is filled with missing data and sent
Intent startActivityIntent = new Intent(context, SimplePersonDetailActivity.class);
startActivityIntent.setData(Uri.parse(startActivityIntent.toUri(Intent.URI_INTENT_SCHEME)));
startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(android.R.id.list, pendingIntent);
// all hail to Google
widgetManager.updateAppWidget(widgetId, views);
}
}
FACTORY
public class WidgetFactory implements RemoteViewsService.RemoteViewsFactory {
private Context context;
private List<Person> people = new ArrayList<>();
public WidgetFactory(Context context, Intent intent) {
this.context = context;
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
WidgetPreferences prefs = new WidgetPreferences(context);
WidgetDataCategory category = prefs.getSavedCategory(widgetId);
int numberOfItemsToShow = prefs.getSavedLimit(widgetId);
people = category.filterAndSlice(new PersonDao(context).getAllForGroup(Constants.SIMPLE_GROUP_ID), numberOfItemsToShow);
}
}
#Override
public void onCreate() {}
#Override
public void onDataSetChanged() {}
#Override
public void onDestroy() {}
#Override
public int getCount() {
return people.size();
}
#Override
public RemoteViews getViewAt(int position) {
Person person = people.get(position);
BigDecimal amount = ListViewUtil.sumTransactions(new TransactionDao(context).getAllForPerson(person));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.item_widget);
remoteViews.setTextViewText(R.id.nameText, person.getName());
remoteViews.setTextViewText(R.id.amountText, MoneyFormatter.withoutPlusPrefix().format(amount));
// fill details for the onclick listener (updating the pending intent template
// set in the WidgetProvider)
Intent listenerIntent = new Intent();
listenerIntent.putExtra(Constants.PERSON_ID, people.get(position).getId());
remoteViews.setOnClickFillInIntent(R.id.widgetItem, listenerIntent);
return remoteViews;
}
#Override
public RemoteViews getLoadingView() {
return null;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean hasStableIds() {
return true;
}
}
I would say that notifyAppWidgetViewDataChanged method should work for you.
You have to build AppWidgetManager and get appWidgetIds and then just call notifyAppWidgetViewDataChanged on your AppWidgetManager.
Pseudo Code,
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetIds[] = appWidgetManager.getAppWidgetIds(
new ComponentName(context, WidgetProvider.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview);
For more, you can checkout my answer here which contains demo on github.
I had a widget implementation in my project. I have modified the below code so that data in widget can be changed from one of my Activity in application. Only showing the essential code for your specific use case. Here I am having a button with text in my widget. Through login button click in my Activity , I am modifying the button text in my widget
Below is my AppWidgetProvider.java class
public class AppWidgetTrackAsset extends AppWidgetProvider{
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// Perform this loop procedure for each App Widget that belongs to this provider
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch Activity
Intent intent = new Intent(context, WidgetAlertActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget_track_asset);
views.setOnClickPendingIntent(R.id.sosButton, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.v(Constants.WIDGET_LOG, "onReceive called with " + intent.getAction());
if (intent.getAction().equals("update_widget")) {
// widget update started
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.app_widget_track_asset);
// Update text , images etc
remoteViews.setTextViewText(R.id.sosButton, "My updated text");
// Trigger widget layout update
AppWidgetManager.getInstance(context).updateAppWidget(
new ComponentName(context, AppWidgetTrackAsset.class), 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
}
}
Below is my Activity where I am updating the widget button text on click of my login button
LoginActivity.java
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton=(Button)findViewById(R.id.loginButton);
loginButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.loginButton){
updateWidget();
}
}
private void updateWidget(){
try {
Intent updateWidget = new Intent(this, AppWidgetTrackAsset.class);
updateWidget.setAction("update_widget");
PendingIntent pending = PendingIntent.getBroadcast(this, 0, updateWidget, PendingIntent.FLAG_CANCEL_CURRENT);
pending.send();
} catch (PendingIntent.CanceledException e) {
Log.e(Constants.UI_LOG,"Error widgetTrial()="+e.toString());
}
}
}
Layout for app widget goes like this
app_widget_track_asset.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/widgetbackground"
android:padding="#dimen/widget_margin">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sosButton"
android:text="SOS"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Below is the manifest file essential part for widget
<receiver android:name=".appwidget.AppWidgetTrackAsset">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/app_widget_track_asset_info" />
</receiver>

Android Widget - works first time only

I have a widget, that on click of a text view, launches a new activity which is an editText to allow the user to search. After you search, it opens the app at the desired results. If the user puts the app in the background, and does not press back, when the user goes to click on the widget again, instead of bring up the search editText view, it opens the app to where it left off.
Ideally I want to bring up the search box everytime, even if it overlays the app behind it. Any thoughts? Code is below:
SearchWidgetClass:
public class SearchWidget extends AppWidgetProvider {
private static final String SHOW_POPUP_DIALOG_ACTION = "launch.search.widget";
private static final String LAUNCH_WIDGET = "launch.homescreen.widget";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context, SearchWidget.class);
int[] allWidgetInstancesIds = AppWidgetManager.getInstance(context).getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetInstancesIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.search_widget_layout);
// Create an intent that when received will launch the search bar
Intent searchIntent = new Intent(context, SearchWidget.class);
searchIntent.setAction(SHOW_POPUP_DIALOG_ACTION);
// Create an intent that when received will open the app on the home screen
Intent WidgetIntent = new Intent(context, SearchWidget.class);
WidgetIntent.setAction(LAUNCH_WIDGET);
PendingIntent searchPendingIntent = PendingIntent.getBroadcast(context, widgetId, searchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent openPendingIntent = PendingIntent.getBroadcast(context, widgetId, WidgetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Set up the onClickListener of the widget
// Now, when the widget is pressed the pendingIntent will be sent
remoteViews.setOnClickPendingIntent(R.id.widget_search_textview, searchPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.widget_open, openPendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}//End of For Each loop
super.onUpdate(context, appWidgetManager, appWidgetIds);
}//End of onUpdate method
#Override
public void onReceive(final Context context, Intent intent) {
Intent actionIntent;
if (intent.getAction().equals(SHOW_POPUP_DIALOG_ACTION)) {
actionIntent = new Intent(context, SearchWidgetDialogActivity.class);
actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(actionIntent);
} else if (intent.getAction().equals(LAUNCH_WIDGET)){
actionIntent = new Intent(context, HomeActivity.class);
actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(actionIntent);
}
super.onReceive(context, intent);
}//End of onReceive method
}//End of SearchWidget Class
Search Dialog Activity:
public class SearchWidgetDialogActivity extends Activity {
private EditText searchWidgetEditText;
private String actionArg;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.search_widget_dialog_layout);
LayoutParams params = getWindow().getAttributes();
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
searchWidgetEditText = (EditText) findViewById(R.id.search_widget_edit_text);
searchWidgetEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) &&
(!StringUtils.isEmpty(searchWidgetEditText.getText().toString()))) {
handleWidgetAction(SearchWidgetDialogActivity.this);
}
return false;
}
});
}//End of onCreate method
public void handleWidgetAction(Context aContext) {
actionArg = searchWidgetEditText.getText().toString();
QLog.d("Launching search activity for " + actionArg + " from the widget");
Intent serachProductsIntent = new Intent(aContext, BrowseProductsSearchActivity.class);
serachProductsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
serachProductsIntent.setAction(Intent.ACTION_SEARCH);
serachProductsIntent.putExtra(SearchManager.QUERY, actionArg);
serachProductsIntent.putExtra(SearchManager.USER_QUERY, actionArg);
aContext.startActivity(serachProductsIntent);
}//End of handleWidgetSearch method
}// End of SearchWidgetDialogActivity class
Manifest Activities:
<activity
android:name="android.activities.SearchWidgetDialogActivity"
android:label="#string/app_label"
android:theme="#android:style/Theme.Dialog"
android:noHistory="true"
android:windowSoftInputMode="adjustResize">
</activity>
<receiver
android:name="android.widget.SearchWidget" >
<intent-filter>
<action android:name="launch.search.widget"/>
<action android:name="launch.homescreen.widget"/>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/search_widget_provider" />
</receiver>

Cannot get Widget ID in Activity

I am new to this and this is a big headache for me. I have a widget with a provider, a config and an extra activity which is called when you click on the widget. I need to determine the widget ID in the activity.
In the provider I set it to call the activity from onUpdate with a .setOnClickPendingIntent, and in the intent I add the widget ID. When I try to get the widget ID from the intent in the activity, it is always 0 (but the activity is called fine). I can retrieve the ID in the config but not in the activity with the same code.
I'm sure it will be something basic/an amateur mistake. Your help is appreciated!
Truncated code:
Provider:
public static String ACTION_WIDGET_CLICK = "ClickWidget";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context,
HelloWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent clickIntent = new Intent(context, test.class);
clickIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
clickIntent.setAction(ACTION_WIDGET_CLICK);
PendingIntent clickPendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
views.setOnClickPendingIntent(R.id.update, clickPendingIntent);
appWidgetManager.updateAppWidget(widgetId, views);
}
}
So on click, it will call the pendingIntent which will call the activity defined by ACTION_WIDGET_CLICK. Manifest:
<activity android:name=".test">
<intent-filter>
<action
android:name="com.example.widget_create.HelloWidgetProvider.ACTION_WIDGET_CLICK"/>
</intent-filter>
</activity>
So this calls the test actvity:
public class test extends Activity {
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent clickIntent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
}
It doesn't receive the extras and then the widget ID gets set to 0
If the same value is given by getExtras in (for example) the activity, you must change the requestCode (perhaps to the widgetid)
Intent clickIntent = new Intent(context,ActivityName.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,myWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, myWidgetId, clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.layout,pendingIntent);
Can you copy/paste what you get into the console log please ?
Check your Manifest file for the android:name tag, it must be android:name=".Test" with an uppercase.
At onUpdateyou are passing IDS (array of ID)
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
while at your activity your are asking por ID (just one ID)
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
As you are inside a for looping throught every widget ID, you should change the intent at the provider with:
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
Notice the EXTRA_APPWIDGET_ID and the widgetId value instead of the old appWidgetIds.

Categories

Resources