【发布时间】:2015-06-13 16:55:31
【问题描述】:
更新:已解决,解决方法如下
我正在尝试编写一个启动服务的小部件,然后它将执行一些尚未实现的东西。到目前为止,我的服务只是:
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 并在此之后自行停止。
不幸的是,事实并非如此。我的 Provider 看起来像这样:
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。如果没有得到处理,小部件似乎不会将点击传递到下面的视图。
【问题讨论】:
-
你看到我的回答了吗?
-
是的,我有,正在吃东西,然后需要一些时间来尝试一下;-)
标签: android android-intent android-widget android-service android-broadcast