通过单击小部件开始活动

Start activity by clicking on widget(通过单击小部件开始活动)

本文介绍了通过单击小部件开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 编程的新手,我尝试做一个小部件,它能够从 ISP 获取一些关于我的帐户的数据.有很多未知的事情怎么做,但我做了一些事情——我有一个带有配置活动的小部件,用户应该在其中输入登录名和密码.小部件将数据存储在 SharedPerferences 中,当需要更新小部件时,我使用服务启动 AsyncTask 以从 ISP 获取帐户数据.现在我想通过单击小部件来启动一个活动.我已经尝试了在此站点上找到的所有建议,并且小部件无法启动活动.我的小部件基于放置在这里的另一个小部件 https://github.com/Arturus/MetrikaWidget.我不明白我应该通过单击我的小部件来更改什么以及在哪里开始活动.谢谢.

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.

更新:我的更新功能,我建议我应该放置 PendingIntent

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);
}

推荐答案

在您的小部件 AppWidgetProvider 类的 onUpdate() 方法中使用此代码段:

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);
}     

这里 widgetlayout 是您的小部件布局的名称,而 R.id.widget 是它的父布局 ID.

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.

参考:
doityourselfandroid.com

helloandroid.com

这篇关于通过单击小部件开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过单击小部件开始活动

基础教程推荐