本文介绍了请帮助非常简单的Andr​​oid小工具按钮的点击。变得非常沮丧。 :(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经倒在每一个例子,我能找到,我已经通过从谷歌官方文档不见了。所有我想要做的是从一个按钮,点击一个小部件创建一个Toast通知。

I have poured over every example that I can find and I have gone through the official documentation from google. All I am trying to do is create a toast notification from a button click on a widget.

我可以得到的小部件(和按钮)出现,但我不能让我的意图火灾。我看到这个节目这样做不同的方法的几个例子,但我不能得到它的工作。我没有感觉到这无奈与编码在很长一段时间,我开始觉得愚蠢。

I can get the widget (and button) to appear but I can't get my intent to fire. I have seen several examples that show doing this different ways but I just can't get it to work. I haven't felt this helpless with coding in a long time and I'm starting to feel dumb.

这是我到目前为止有:

public class simpleclick extends AppWidgetProvider {
    /** Called when the activity is first created. */

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        Toast.makeText(context, "doing on update", Toast.LENGTH_SHORT).show();

        for (int i=0; i<N; i++) {

            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, simpleclick.class);
            intent.setAction("ham");
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
            views.setOnClickPendingIntent(R.id.Timm, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);

        }

    }

    //@Override
    public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub

     Toast.makeText(context, "action is: " + intent.getAction(), Toast.LENGTH_SHORT).show();

     super.onReceive(context, intent);

    }

}

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tblabs.simpleclick"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name="simpleclick">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" 
            android:resource="@xml/simpleclick" />
        </receiver>

    </application>
    <uses-sdk android:minSdkVersion="5" />

</manifest> 

我会AP preciate一些帮助!

I would appreciate some help!

谢谢,温迪

推荐答案

由于 bigstones 写的,你需要使用广播。样品code:

As bigstones wrote, you need to use broadcast.Sample code:

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}

views.setOnClickPendingIntent(R.id.Timm, getPendingSelfIntent(context,
                              "ham"));

这篇关于请帮助非常简单的Andr​​oid小工具按钮的点击。变得非常沮丧。 :(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:02