1

I have this widget that toggles sound on off but instead of that I want to call another activity (MYxz.class) please tell me what should I change here...

  public class AppWidget extends AppWidgetProvider {

 @Override
 public void onReceive(Context ctxt, Intent intent)
 {
      if(intent.getAction()==null)
      {
          ctxt.startService(new Intent(ctxt,ToggleService.class));
      }
   else
      {
    super.onReceive(ctxt, intent);
}

}

   @Override
  public void onUpdate(Context context,AppWidgetManager appWidgetManager, int [] appWidgetIds)
  {
context.startService(new Intent(context,ToggleService.class));
//RemoteViews buildUpdate(context);
   }

  public static class ToggleService extends IntentService
   {
public ToggleService() {
    super("AppWidget$ToggleService");

    }



    @Override
    protected void onHandleIntent(Intent intent)
 {
   ComponentName me = new ComponentName(this,AppWidget.class);
  AppWidgetManager mgr= AppWidgetManager.getInstance(this);
  mgr.updateAppWidget(me,buildUpdate(this));
 }

 private RemoteViews buildUpdate(Context context)
{
RemoteViews updateViews=new RemoteViews(context.getPackageName(),R.layout.widget);
AudioManager audioManager=(AudioManager)context.getSystemService(Activity.AUDIO_SERVICE);
if(audioManager.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
    updateViews.setImageViewResource(R.id.phoneState,R.drawable.silent);
    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

}
else {
    updateViews.setImageViewResource(R.id.phoneState,R.drawable.phone123);
    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

}
Intent i=new Intent(this, AppWidget.class);
PendingIntent pi= PendingIntent.getBroadcast(context,0, i,0);
updateViews.setOnClickPendingIntent(R.id.phoneState,pi);
return updateViews;
}

 }
}
Yury
  • 20,618
  • 7
  • 58
  • 86
denza
  • 1,298
  • 4
  • 24
  • 49

3 Answers3

2

Yes possible see example:

Upadte your manifest.xml

<receiver android:name=".AppWidget"
        android:label="Caller"
        android:icon="@drawable/ic_launcher" >
        <intent-filter >
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
 <action android:name="com.app.example.MyWidget.ACTION_WIDGET_CLICK_RECEIVER"/>

        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_provider"
            />

    </receiver>
    <service android:name=".AppWidget$ToggleService"  />

and Update your AppWidgetProvider:

      public class MyWidget extends AppWidgetProvider {
                    public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget";

                    public static int appid[];
                    public static RemoteViews rview;
                    @Override
                    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
                            int[] appWidgetIds){
                        updateWidgetState(context, ""); 
                    }
                    @Override
                    public void onReceive(Context paramContext, Intent paramIntent)
                      {
                         String str = paramIntent.getAction();
                        if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) {
                            updateWidgetState(paramContext, str);   
                        }
                        else
                        {
                                if ("android.appwidget.action.APPWIDGET_DELETED".equals(str))
                                  {
                                    int i = paramIntent.getExtras().getInt("appWidgetId", 0);
                                    if (i == 0)
                                    {

                                    }
                                    else
                                    {
                                        int[] arrayOfInt = new int[1];
                                        arrayOfInt[0] = i;
                                        onDeleted(paramContext, arrayOfInt);
                                    }
                                  }
                          super.onReceive(paramContext, paramIntent);
                        }
                      }
                     static void updateWidgetState(Context paramContext, String paramString)
                      {
                        RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
                        ComponentName localComponentName = new ComponentName(paramContext, MyWidget.class);
                        AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
                      }
                     private static RemoteViews buildUpdate(Context paramContext, String paramString)
                      {
                        // Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show();
                        rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout);
                        Intent active = new Intent(paramContext, MyWidget.class);
                        active.setAction(ACTION_WIDGET_RECEIVER);

                        PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0);

        // upadte this R.id.buttonus1 with your layout or image id on which click you want to start Activity

    Intent configIntent = new Intent(paramContext, Caller2.class);
     configIntent.setAction((ACTION_WIDGET_CLICK_RECEIVER);
     PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, configIntent, 0);
rview.setOnClickPendingIntent(R.id.Phonestatexx, configPendingIntent);
                        if(parmString.equals(ACTION_WIDGET_CLICK_RECEIVER))
                        {

                           //open Activity here..
                         //your code for update and what you want on button click
            //

                        }  
                         return rview; 
                      }
                    @Override
                    public void onEnabled(Context context){
                        super.onEnabled(context);
                       // Toast.makeText(context, "onEnabled()  ", Toast.LENGTH_SHORT).show();
                    }
                    // Called each time an instance of the App Widget is removed from the host
                    @Override
                    public void onDeleted(Context context, int [] appWidgetId){
                        super.onDeleted(context, appWidgetId);
                       // Toast.makeText(context, "onDeleted()  ", Toast.LENGTH_SHORT).show();
                    }
                    // Called when last instance of App Widget is deleted from the App Widget host.
                    @Override
                    public void onDisabled(Context context) {
                        super.onDisabled(context);
                       // Toast.makeText(context, "onDisabled()  ", Toast.LENGTH_SHORT).show();
                    }

                }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • hi denza see my solution. this is working example on my side if you have any issue then say me in running this code – ρяσѕρєя K Mar 12 '12 at 17:48
  • 1
    Toast.makeText(paramContext,"nesto tost",Toast.LENGTH_SHORT).show(); i tried to put a toast under if(parmString.equals(ACTION_WIDGET_CLICK_RECEIVER)) but it didn't show and i cant create an intent new Intent(this,Caller2.class) it shows error on this – denza Mar 13 '12 at 09:12
  • and one more point:replace this `R.id.buttonus1` id with layout or control id on which click you want to open new Activity – ρяσѕρєя K Mar 13 '12 at 09:37
  • and prepare as Intent active = new Intent(paramContext, Caller2.class); for starting Activity – ρяσѕρєя K Mar 13 '12 at 09:38
  • 1
    I did change it.. can u please write me i want to do this but it's not workig can u please edit the answer with this happening on click: i want Intent i= new Intent(this, Caller2.class) startActivity(i); THX – denza Mar 13 '12 at 09:40
  • ok tell me Layout or button id on which click you want to open Activity? – ρяσѕρєя K Mar 13 '12 at 09:42
  • 1
    if(paramString.equals(ACTION_WIDGET_CLICK_RECEIVER)) { Intent i= new Intent(paramContext,Caller2.class); startActivity(i); } return rview; } ive wrote down this but it shows error on startActivity()//create a metod start activity – denza Mar 13 '12 at 09:46
  • 1
    Instead of button i have an imageview does it make a problem I can change to button ... – denza Mar 13 '12 at 09:47
  • 1
    and also this line active.setAction(ACTION_WIDGET_RECEIVER); should it be ACTION_WIDGET_CLICK_RECEIVER – denza Mar 13 '12 at 09:54
  • see my edit last change if not then follow [hellowidget](http://www.helloandroid.com/files/xmaswidget/android_howto-hellowidget.pdf) – ρяσѕρєя K Mar 13 '12 at 10:03
1

With using pendingIntents you can call an intent (to your activity or sth else) when a widget item clicked. this may help: http://www.vogella.de/articles/AndroidWidgets/article.html

Kayhan Asghari
  • 2,817
  • 1
  • 28
  • 47
  • And this is an original android sample: http://developer.android.com/resources/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetProvider.html – Kayhan Asghari Mar 12 '12 at 17:33
1

Instead of:

Intent active = new Intent(paramContext, AppWidget.class);

You use:

Intent active = new Intent(paramContext, YOURCLASS.class); 

before you create the pending intent, @imran khan helped me but there are some tweaks you should do 2...this should fire up the Activity you need.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Tony
  • 1,175
  • 4
  • 14
  • 22