Android widget ListView item clicks not working - android

So I'm trying to set up a widget with a list but clicks aren't working, so I understand to do this I need a template pending intent as individual items cannot have their own, and then add to the pending intent template using a fill in intent, I've added all this to my app but it still isn't working, I've followed advice here and here but when I click on an item its just highlighted to show it has registered a click and does nothing but I do get this in the
logcat
08-06 17:02:08.001 2163-3666/? W/Binder: Binder call failed.
java.lang.NullPointerException: Attempt to read from field
'com.android.server.appwidget.AppWidgetServiceImpl$ProviderId
com.android.server.appwidget.AppWidgetServiceImpl$Provider.id'
on a null object reference
at
com.android.server.appwidget.AppWidgetServiceImpl$AppWidgetManagerLocal
.getHostedWidgetPackages(AppWidgetServiceImpl.java:4800)
at
com.android.server.accessibility.AccessibilityManagerService$SecurityPolicy
.resolveValidReportedPackageLocked(AccessibilityManagerService.java:4637)
at com.android.server.accessibility.AccessibilityManagerService
.sendAccessibilityEvent(AccessibilityManagerService.java:531)
at android.view.accessibility.IAccessibilityManager$Stub
.onTransact(IAccessibilityManager.java:71)
at android.os.Binder.execTransact(Binder.java:690)
so here is my code, my onUpdate method calls a static update widget method that defines my template pending intent, and my onReceive method
AppWidgetProvider
public class MessageMeAppWidget extends AppWidgetProvider {
private static final String TAG = "MsgMeWdgt";
#Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(Constants.LAUNCH_ACTIVITY)) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
//String recipientId = intent.getStringExtra(Constants.FS_ID);
//String recipientName = intent.getStringExtra(Constants.FS_NAME);
Intent i = new Intent(context, MessageListActivity.class);
Bundle b = new Bundle();
//b.putString(Constants.FS_ID, recipientId);
//b.putString(Constants.FS_ID, recipientName);
i.putExtras(b);
Bundle bundle = intent.getExtras();
String toastText = "position is " + bundle.getInt(Constants.LAUNCH_ACTIVITY);
Toast.makeText(context,toastText,Toast.LENGTH_SHORT).show();
//context.startActivity(i);
}else if(intent.getAction().equals(Constants.DONT_LAUNCH_ACTIVITY)){
//Toast.makeText(context,"",Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SHARED_PREFS,Context.MODE_PRIVATE);
boolean userSignedIn = sharedPreferences.getBoolean(Constants.SIGNED_IN, false);
if (userSignedIn){
//move everything in here after i know its working
}
Intent intent = new Intent(context, MessagingService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_list_view);
//populates data
remoteViews.setRemoteAdapter(R.id.widget_list,intent);
//remoteViews.setRemoteAdapter(appWidgetId, R.id.widget_list, intent);
remoteViews.setEmptyView(R.id.widget_list,R.id.empty);
Intent launchIntent = new Intent(context, WidgetAdapter.class);
launchIntent.setAction(Constants.LAUNCH_ACTIVITY);
launchIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent launchPendingIntent = PendingIntent.getBroadcast(context, 0, launchIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.widget_list, launchPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
Log.d(TAG,"on update");
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
Then my widget service is standard
public class MessagingService extends RemoteViewsService {
private String TAG = "MsgSrvc";
#Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
Log.d(TAG,"item clicked");
return new WidgetAdapter(this.getApplicationContext(), intent);
}
}
and then in the getViewAt method of my widget adapter I fill my fill in intent like this
Bundle extras = new Bundle();
extras.putInt(Constants.LAUNCH_ACTIVITY, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
remoteViews.setOnClickFillInIntent(R.id.holder, fillInIntent);
in my manifest I'm registering my widget service and the receiver
<receiver android:name="com.sealstudios.aimessage.MessageMeAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/message_me_app_widget_info" />
</receiver>
<service android:name=".MessagingService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
any help appreciated

i fixed this it wasthe intent was pointing to the wrong class so
Intent launchIntent = new Intent(context, WidgetAdapter.class);
should have been
Intent launchIntent = new Intent(context, MessageMeAppWidget.class);

Related

Change an Android intent action from opening URI to displaying a toast?

I'm trying to have my widget use an onClick action to prompt a refresh. I found some demo code that works, but it opens a URI in browser. I'm having trouble replacing the URI action with something simpler, like displaying a toast. I've marked the line that I'm trying to change, but I don't know how to make Android set a different action other than Action.VIEW.
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
//I AM TRYING TO CHANGE THIS INTENT
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.refresh, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override //Nothing changed here
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
}
The specific lines are
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
I got this to work completely unexpectedly. I followed this post but left my intent declarations where they were. The resulting lines were:
Intent intent = new Intent(context, NewAppWidget.class);
intent.setAction(refresh);
intent.putExtra("appWidgetId", appWidgetId);
views.setOnClickPendingIntent(R.id.refresh, PendingIntent.getBroadcast(context,0,intent, PendingIntent.FLAG_UPDATE_CURRENT));
appWidgetManager.updateAppWidget(appWidgetId, views);
followed by onReceive:
#Override
public void onReceive(Context context, Intent intent) {
if(refresh.equals(intent.getAction())) {
Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
}
}
Edit for clarity: intent.getAction() needs to be a registered action within the receiver's intent filter.

How to open another view in a widget that stays on the homescreen?

I was using the Guardian news app and noticed that their widget had an options button that opened anther menu. I am not sure how this is done. If anyone has any ideas that would great.
Guardian Widget
Options menu open
This easily way it's open a dialog or a modal pop-up activity.
You can send an Intent as usual:
public class YourWidget extends AppWidgetProvider {
static PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
KLog.d(TAG, "getPendingSelfIntent appWidgetId = " + appWidgetId);
Intent intent = new Intent(context, YourWidget.class);
Bundle b = new Bundle();
b.putInt("appWidgetId", appWidgetId);
intent.putExtras(b);
intent.setAction(action);
PendingIntent pendInt = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendInt;
}
//....
//your method for remote views
public void doUpdateView() {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.collection_widget);
remoteViews.setOnClickPendingIntent(R.id.button_settings, getPendingSelfIntent(context, RUN_CLICKED, appWidgetId));
}
#Override
public void onReceive(final Context context, Intent intent) {
super.onReceive(context, intent);
if (RUN_CLICKED.equals(intent.getAction())) {
Intent actIntent = new Intent(context, YourActivity.class);
actIntent.putExtra("your_extra_key", your_extra_value);
actIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(actIntent);
}
//...
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}

update textView in a widget

I've read a lot of Q&As but I couldn't find my answer.
May be something in my implementation is wrong.
The problem is that my TextView in the widget doesn't get updated.
The logic is this:
1.setOnClickPendingIntent on a specific button in the onUpdate()
2.clicking on this button will broadcast an intent with a declared action
3.at last i'll update the text of the textView in the onRecieve()
Widget2.class :
public class Widget2 extends AppWidgetProvider {
private static final String SCROLL_LEFT = "widget2.SCROLL_LEFT";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small);
Intent scrollLeft = new Intent(Widget2.SCROLL_LEFT);
PendingIntent leftScrollPendingIntent = PendingIntent.getBroadcast(context,
appWidgetId, scrollLeft, appWidgetId);
remoteViews.setOnClickPendingIntent(R.id.left_scroller, leftScrollPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
#Override
public void onReceive(Context context, Intent intent) {
Log.e(getClass().getSimpleName(), "onReceive()");
int appWidgetId = intent.getFlags();
if (intent.getAction().equals(SCROLL_LEFT)) {
updateCurrentWidget(context, appWidgetId);
Log.i("onReceive", SCROLL_LEFT + " appWidgetId = " + appWidgetId);
}
super.onReceive(context,intent);
}
private void updateCurrentWidget(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small);
remoteViews.setString(R.id.name_of_the_app, "setText", "android os");
remoteViews.setTextViewText(R.id.description, "best ever");
Log.i("updateCurrentWidget", "the text have been set");
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(appWidgetId, remoteViews);
}
manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".Widget2" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="widget2.SCROLL_LEFT" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_small" />
</receiver>
</application>
</manifest>
and here is the simplified logcat logs:
onReceive()
"myPackageName".Widget2.SCROLL_LEFT
the text have been set
"myPackageName".Widget2.SCROLL_LEFT appWidgetId = 268435456
everything seems to be correct but the text is never changed!
The key in the widget is you have to set every View and every Intent before calling the updateAppWidget() method. Here You can call updateWidgetInstance(...) from your onReceive() method:
// action = the String that you get from your intent in the onRecive() method
// id = the id of the appWidget instance that you want to update
// you can get the id in the onReceive() method like this:
// int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
private static void updateWidgetInstance(Context context, AppWidgetManager manager, String action, int id) {
RemoteViews remoteViews = updateCurrentWidget(context, id);
remoteViews = setIntents(remoteViews, context, id);
manager.updateAppWidget(id, remoteViews);
}
updating the View: (Here you can also change the layout of this instance depending on your needs)
private static RemoteViews updateCurrentWidget(Context context, int appWidgetId) {
RemoteViews remoteViews;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_small_layout);
remoteViews.setTextViewText(R.id.widget_name_of_the_app, "Android OS");
remoteViews.setTextViewText(R.id.widget_app_description, "best ever");
remoteViews.setImageViewUri(R.id.widget_icon_of_the_app, ...);
return remoteViews;
}
updating intents:
private static RemoteViews setIntents(RemoteViews rm, Context context, int appWidgetId) {
Intent click = new Intent(context, Widget.class);
click.setAction("[name of the action]");
click.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, appWidgetId, click, PendingIntent.FLAG_UPDATE_CURRENT);
rm.setOnClickPendingIntent(R.id.button1, pendingIntent);
return rm;
}
you are setting your appWidgetId in the PendingIntent flags. that's just wrong since PendingIntet flags have meanings, they are not meant to save some data.
You should use the extras for that.

cant pass widget id to configuration activity

I wrote an AppWidget that has a configuration activity(I used the same configuration activity of the app itself)
When adding the Widget to the home screen while on debug mode I pass the widget id(using put extra) to the intent. when clicking on the widget itself(to load the prefs' activity I break at the onCreate method, at the parts where I'm calling intent.getExtras or intent.getIntExtra - I get null.
I wanted to use the following code but couldn;t understand how:
passing-widget-id-to-activity:
The issue was that android does caching with PendingIntents. The solution was to add the FLAG_UPDATE_CURRENT flag which causes it to update the cached PendingIntent.
PendingIntent configPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE_ONE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
here is my code:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.dryrun" android:versionCode="3"
android:versionName="1.1" android:installLocation="auto">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/ic_launcher_test"
android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:debuggable="true"><!-- different< android:theme="#style/Theme.NoBackground" -->
<!-- Main Activity -->
<activity android:name=".MyActivity"
android:configChanges="orientation"> <!--android:screenOrientation="portrait" -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Preferences -->
<activity android:name=".Preferences.EditPreferences"
android:configChanges="orientation">
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</activity>
<!-- Widgets -->
<!-- Widget-->
<receiver android:name=".Widget.testWidget" android:label="#string/app_widget_">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!--action
android:name="com.test.dryrun.Widget.testWidget.PREFENCES_WIDGET_CONFIGURE" /-->
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/test_widget__provider" />
</receiver>
<service android:name=".Widget.testWidget$WidgetService" />
<uses-permission android:name="android.permission.BIND_REMOTEVIEWS"></uses-permission>
</application>
</manifest>
appwidget_provider xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="146dip"
android:updatePeriodMillis="0"
android:initialLayout="#layout/test_widget_"
/>
Widget Class
public class testWidget extends AppWidgetProvider {
public static String PREFENCES_WIDGET_CONFIGURE = "ActionConfigureWidget";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Intent svcIntent = new Intent(context, WidgetService.class);
context.startService(svcIntent);
}
#Override
public void onReceive(Context context, Intent intent)
{
RemoteViews remoteViews = new RemoteViews(
context.getPackageName(), R.layout.test_widget);
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
this.onDeleted(context, new int[] { appWidgetId });
}
}
else
{
super.onReceive(context, intent);
}
}
//public void updateWidget()
/**
* #param context
* #param remoteViews
*/
public static void updateWidget(Context context, RemoteViews remoteViews)
{
String Prefix = context.getString(R.string._prefix);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String ToShow = prefs.getString(context.getString(
R.string.Widget_string),
context.getString(R.string.default_string));
String pkgName = context.getPackageName();
int resID = context.getResources().getIdentifier(Prefix + ToShow, "drawable", pkgName);
WidgetController widgetController = WidgetController.getInstance();
widgetController.setRemoteViewImageViewSource(remoteViews, R.id.WidgetImage, resID);
}
public static class WidgetService extends Service
{
#Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
WidgetController.getInstance().pushUpdate(
remoteView,
getApplicationContext(),
testWidget.class);
// No more updates so stop the service and free resources
stopSelf();
}
public RemoteViews buildRemoteView(Context context)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.test_widget_);
Intent runConfigtest = new Intent(context, EditPreferences.class);
runConfigtest.setAction(testWidget.PREFENCES_WIDGET_CONFIGURE);
//old code-what you get in all the widget examples
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, runConfigtest, 0);
//new code - this is how you should write it
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, runConfigtest, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.WidgetImage, runtestPendingIntent);
updateWidget(context, remoteViews);
return remoteViews;
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
int oldOrientation = this.getResources().getConfiguration().orientation;
if(newConfig.orientation != oldOrientation)
{
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
WidgetController.getInstance().pushUpdate(
remoteView,
getApplicationContext(),
testWidget.class);
}
}
#Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
}
}
Prefences class
public class EditPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Intent intent = getIntent();
m_extras = intent.getExtras();
mAppWidgetId = intent.getIntExtra("widget_id", defaultVal);
}
private Bundle m_extras;
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if(key.equals(getString(R.string.rlvntString)))
{
Context ctx = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
setResult(RESULT_CANCELED);
if (m_extras != null)
{
mAppWidgetId = m_extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
RemoteViews views = new RemoteViews(ctx.getPackageName(),
R.layout.test_widget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
else
{
RemoteViews views = new RemoteViews(ctx.getPackageName(),
R.layout.test_widget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
}
I needed to change the next thing:
//old code-what you get in all the widget examples
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, runConfigtest, 0);
//new code - this is how you should write it
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, runConfigtest, PendingIntent.FLAG_UPDATE_CURRENT);
now it works

Android simple widget that launches activity

Hi i've never worked with widgets before but what i'm looking to do is create a very simple widget i basically want to make a 1 by 1 widget that has just an icon, just an image set as the background no text nothing just a small icon and when the icon is pressed i want to open an activity. Basically i want to make a second icon like in the app drawer in a widget form that opens another activity rather than the main one.
Any help is greatly appreciated
My provider ended up looking like this after a lot of research and playing
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, ClassToLaunchHere.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Couple of app widget implementations that show the easiest way to do that are visible at https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget.
Specifically, https://github.com/commonsguy/cw-advandroid/blob/master/AppWidget/PairOfDice/src/com/commonsware/android/appwidget/dice/AppWidget.java shows how to use a PendingIntent as the onClick target for a Button. You can make your PendingIntent start an Activity and you should be good to go.
Declare a Variable
public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";
then add onUpdate and onReceive
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context, DigitalClock.class);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.digital_clock);
for (int widgetId : appWidgetManager.getAppWidgetIds(thisWidget)) {
remoteViews.setOnClickPendingIntent(R.id.imageView, getPendingSelfIntent(context, YOUR_AWESOME_ACTION));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if (YOUR_AWESOME_ACTION.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.digital_clock);
ComponentName watchWidget = new ComponentName(context, DigitalClock.class);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
Intent ntent = new Intent(context, MainActivity.class);
ntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ntent);
//Toast.makeText(context, YOUR_AWESOME_ACTION, Toast.LENGTH_SHORT).show();
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
and add activity on Manifest
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
hope this will help

Categories

Resources