A simple Android widget that launches activity

Hi, I have never worked with widgets before, but what I want to do is create a very simple widget in which I want to make a 1 to 1 widget that has only an icon, just an image set as background text will not just a small icon, and when the icon is clicked, I want to open the action. Basically, I want to make a second icon, like in an application box, in the form of widgets that opens up another action, not the main one. Any help is appreciated.

+3
source share
3 answers

My provider turned out to be the same after a lot of research and games.

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);
        }
    }
}
+9
source

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 to the 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 helps

0
source

All Articles