通过点击窗口小部件启动活动部件、窗口

2023-09-12 04:27:40 作者:孤烟

我新手在编程Android和我尝试​​做一个控件,它具有能够得到关于我的帐户从ISP的一些数据。还有很多未知的东西怎么办呢,但我已经做了一些事情 - 我有与配置的活动,在这里用户应该输入登录名和密码的窗口小部件。小工具店SharedPerferences的数据,当它的时间来更新插件我用服务来启动的AsyncTask从ISP得到它的帐户数据。现在我想做的事情通过点击小部件开始活动。我试过,我发现本网站上的所有意见和窗口小部件无法启动的活动。我的窗口小部件基于另一个控件,它在这里 https://github.com/Arturus/MetrikaWidget 放置。我不明白什么,在哪里我应该改变通过单击我的小部件开始活动。谢谢你。

更新: 我的更新功能,在这里我建议我应该把PendingIntent

  @覆盖
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds)
{
    Log.d(TAG的onupdate);

    对于(INT appWidgetId:appWidgetIds)
    {
        updateAppWidget(背景下,appWidgetManager,appWidgetId);
    }

    / *一个updateAppWidget功能看起来像如下:

    意向意图=新的意图(背景下,UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);
    context.startService(意向);
    * /

    意向意图=新的意图(背景下,DetailedStatActivity.class);
    PendingIntent pendingIntent = PendingIntent.getService(上下文,0,意图,0);

    RemoteViews意见=新RemoteViews(context.getPackageName(),R.layout.layout);

    views.setOnClickPendingIntent(R.id.layout,pendingIntent);

    组件名组件名=新单元名(context.getPackageName(),WidgetProvider.class.getName());

    appWidgetManager.updateAppWidget(组件名,意见);
}
 

解决方案

使用这个片段在小部件的的OnUpdate()方法 AppWidgetProvider 类:

  @覆盖
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
    RemoteViews remoteViews =新RemoteViews(context.getPackageName(),R.layout.widgetlayout);
    意图configIntent =新的意图(背景下,Activity.class);

    PendingIntent configPendingIntent = PendingIntent.getActivity(上下文,0,configIntent,0);

    remoteViews.setOnClickPendingIntent(R.id.widget,configPendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);
}
 
大神教你如何使用EPLAN里的宏值集

下面 widgetlayout 是小部件的布局的名称,而 R.id.widget 是它的父布局ID。

修改: 现在,我看到你添加到您的question.You会做你的code:

  PendingIntent.getActivity(背景下,0,configIntent,0);
 

代替

(即开始的活动)

  PendingIntent.getService(...);
 

这是试图启动service.Good运气。

参考资料: doityourselfandroid.com

helloandroid.com

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 have did a few things - I've got a widget with configure activity, where user should type login and password. Widget stores the data in SharedPerferences, and when it's time to update widget I use a Service to start an AsyncTask to getting it from ISP an account data. Now I want to do start an activity by click on widget. I've tried all advice which I found on this site and widget can't start activity. My widget based on another widget which placed here https://github.com/Arturus/MetrikaWidget. I dont understand what and where I should change to start activity by clicking on my widget. Thanks.

UPDATE: My update function, where I suggest I should place PendingIntent

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
    Log.d(TAG, "onUpdate");

    for (int appWidgetId : appWidgetIds)
    {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }

    /* An updateAppWidget functions looks like as:

    Intent intent = new Intent(context, UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    context.startService(intent);
    */

    Intent intent = new Intent(context, DetailedStatActivity.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout);

    views.setOnClickPendingIntent(R.id.layout, pendingIntent);

    ComponentName componentName = new ComponentName(context.getPackageName(), WidgetProvider.class.getName());

    appWidgetManager.updateAppWidget(componentName, views);
}

解决方案

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 add 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