Android 小部件 onClick 不工作 - 服务无法启动

Android Widget onClick not working - Service won't start

更新:已解决,解决方法如下

我正在尝试编写一个启动服务的小部件,然后该服务将执行一些尚未实现的操作。到目前为止我的服务只有:

public class SmartWifiService extends Service {

private static final String WIDGET_CLICK = "de.regenhardt.smartwifiwidget.WIDGET_CLICK";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Log.e("DEBUG", "Service started");
    Toast.makeText(getApplicationContext(), "Widget clicked", Toast.LENGTH_SHORT).show();
    stopSelf();
    return START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

所以它所做的一切都是发送 Toast 并在之后停止。

不幸的是,它并没有达到那个目的。我的提供商看起来像这样:

public class SmartWifiWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.e("DEBUG", "onUpdate called");
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    Intent clickIntent = new Intent(context, SmartWifiWidgetProvider.class);
    clickIntent.setAction("de.regenhardt.smartwifiwidget.WIDGET_CLICK");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),
            0, clickIntent, 0);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    views.setOnClickPendingIntent(R.id.layout, pendingIntent);
    //for (int ID:appWidgetIds) {
    //    views.setOnClickPendingIntent(ID, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, views);
    //}
    @Override
public void onReceive(Context context, Intent intent) {
    Log.e("DEBUG", "received");
    super.onReceive(context, intent);
    if(intent.getAction().equals("de.regenhardt.smartwifiwidget.WIDGET_CLICK")){
        Log.e("DEBUG", "Click action fits");
        Intent i = new Intent(context.getApplicationContext(), SmartWifiService.class);
        context.startService(i);
    }
}
}

我在这里回答了几个问题,更改了内容,添加了内容,但到目前为止没有任何效果,我仍然不知道为什么。

当我点击小部件时没有动画,但我很确定我的小部件本身是可点击的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/layout"
          android:clickable="true">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/wifi"
    android:clickable="true"
    android:id="+id/widgetImage"/>
</LinearLayout>

也用ImageButton试过了,效果没有变化。

我希望你们能帮助我,我已经坐在这个小东西里好几天了,我的头在旋转(不是真的)。​​

你好,

马龙

编辑:这是我的清单:

<application android:allowBackup="true"
             android:label="@string/app_name"
             android:icon="@drawable/wifi"
             android:theme="@style/AppTheme">
    <receiver android:name=".SmartWifiWidgetProvider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="de.regenhardt.smartwifiwidget.WIDGET_CLICK"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/smart_wifi_widget_info"/>
    </receiver>
    <service android:name="de.regenhardt.smartwifiwidget.SmartWifiService"/>

</application>

编辑 2:更新;更新到我的代码的当前状态,适应 Y.S。'回答。

LogCat 添加小部件后,点击它仍然没有任何反应:

04-08 20:12:30.985  14867-14867/de.regenhardt.smartwifiwidget E/DEBUG﹕ received
04-08 20:12:30.998  14867-14867/de.regenhardt.smartwifiwidget E/DEBUG﹕ received
04-08 20:12:30.998  14867-14867/de.regenhardt.smartwifiwidget E/DEBUG﹕ onUpdate called
04-08 20:12:31.155  14867-14867/de.regenhardt.smartwifiwidget E/DEBUG﹕ received

解法:

吹线是 views.setOnClickPendingIntent(R.id.widgetImage, pendingIntent);,我在那里有 R.id.layout 而不是 widgetImage。如果一个小部件没有得到处理,它似乎不会将点击传递给下面的视图。

您应该将您的服务添加到清单中:

<service android:name="SmartWifiService"></service>

你没有说你是如何调用服务的,所以我可以说需要从一些 Activity 调用 startService(Intent) - 没有它就不会启动。

问题:

要以这种方式开始 Service,您需要使用 PendingIntent.getBroadcast(),而不是 PendingIntent.getService()WIDGET_CLICK 操作需要在 receiver 标签下的应用程序清单中指定,而不是 service 标签。

第 1 步:

替换

Intent clickIntent = new Intent("de.regenhardt.smartwifiwidget.WIDGET_CLICK");

Intent clickIntent = new Intent(context, SmartWifiWidgetProvider.class);
clickIntent.setAction("de.regenhardt.smartwifiwidget.WIDGET_CLICK");

第 2 步:

替换

PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(),
        0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),
        0, clickIntent, 0);

第 3 步:

在清单中,将 action 添加到 receiver 标签并将其从 service 标签中删除:

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/wifi"
    android:theme="@style/AppTheme">

    <receiver android:name=".SmartWifiWidgetProvider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="de.regenhardt.smartwifiwidget.WIDGET_CLICK"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/smart_wifi_widget_info"/>
    </receiver>

    <service android:name="de.regenhardt.smartwifiwidget.SmartWifiService"></service>

</application>

第 4 步:

在小部件的 RemoteViews 上设置 PendingIntent

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

第 5 步:

覆盖 SmartWifiWidgetProvideronReceive() 方法 class:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (intent.getAction().equals("de.regenhardt.smartwifiwidget.WIDGET_CLICK")) {
        Intent i = new Intent(context.getApplicationContext(), SmartWifiService.class)
        startService(i);
    }
}

试试这个。这应该可以正确启动 Service