Start activity by clicking on widget

前端 未结 3 2083
梦如初夏
梦如初夏 2020-12-15 18:18

I newbie at programming Android and I try to do a widget which has be able get some data from ISP about my account. There are a lot of unknown things how to do it, but I hav

3条回答
  •  死守一世寂寞
    2020-12-15 18:33

    Use this snippet in onUpdate() method of your widget AppWidgetProvider class:

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
        Intent configIntent = new Intent(context, Activity.class);
    
        PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
    
        remoteViews.setOnClickPendingIntent(R.id.widget, configPendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }     
    

    Here widgetlayout is name of your widget layout and R.id.widget is it's parent layout id.

    Edit:
    Now,I see your code that you added to your question.You would to do:

    PendingIntent.getActivity(context, 0, configIntent, 0);
    

    (that start's activity) instead of

    PendingIntent.getService(...);
    

    that attempt to starts service.Good luck.

    References:
    doityourselfandroid.com

    helloandroid.com

提交回复
热议问题