Android 通过单击小部件开始活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11554085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:01:33  来源:igfitidea点击:

Start activity by clicking on widget

androidandroid-activityclickwidget

提问by Nova

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.

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

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

更新:我的更新功能,我建议我应该放置 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);
}

回答by hasanghaforian

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

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

@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 widgetlayoutis name of your widget layout and R.id.widgetis it's parent layout id.

widgetlayout是您的小部件布局的名称,R.id.widget它是父布局 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

参考资料:
doityourselfandroid.com

helloandroid.com

helloandroid.com

回答by Kamalone

Intent inet = new Intent(your_action);
inet.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntentNetworkInfo = PendingIntent.getActivity(context, 2, 
            inet, Intent.FLAG_ACTIVITY_NEW_TASK);
remoteViews.setOnClickPendingIntent(you_component_when_the_user_pressing_this_activity_should_start, pIntentNetworkInfo);

回答by stuckedoverflow

I don't know about "Creating widget from another widget". This is out of my knowledge but I suggest you to build your own widget.

我不知道“从另一个小部件创建小部件”。这是我不知道的,但我建议您构建自己的小部件。

Apart from that, calling activity from widget should be using PendingIntent

除此之外,从小部件调用活动应该使用PendingIntent

Here is simple example to do it

这是一个简单的例子

Intent iSetting = new Intent(this, MyConfig.class);
PendingIntent piSetting = PendingIntent.getActivity(this, 0, iSetting, 0);
views.setOnClickPendingIntent(R.id.IdComponent, piSetting);

Or you might need to see this linkand this link

或者您可能需要查看此链接此链接