当我将Midget拖放到桌面上时,我的Midget工作正常。但是,我也希望在按下按钮时从我的TestActivity
界面执行我的小部件操作。
这是我的小工具 -
package com.droideilhan.ultrasimpletaskkiller;
public class USTaskKillerWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverUltraSimpleTaskKillerWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent clicSurBtnKillIntent = new Intent(context, USTaskKillerWidget.class);
clicSurBtnKillIntent.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, clicSurBtnKillIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.btnKill, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else {
// On a cliqué sur le bouton
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
// Liste des packages à ne pas tuer.
List<String> reservedPackages = new ArrayList<String>();
reservedPackages.add("system");
reservedPackages.add("com.android.launcher2");
reservedPackages.add("com.android.inputmethod.latin");
reservedPackages.add("com.android.phone");
reservedPackages.add("com.android.wallpaper");
reservedPackages.add("com.google.process.gapps");
reservedPackages.add("android.process.acore");
reservedPackages.add("android.process.media");
// On tue tous les processus, sauf ceux de la liste
int compteProcessusTues = 0;
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> listeProcessus = am.getRunningAppProcesses();
for(RunningAppProcessInfo processus : listeProcessus) {
//Log.d("TKTKTK", "========="+processus.pid+" : "+processus.processName);
String packageName = processus.processName.split(":")[0];
if (!context.getPackageName().equals(packageName) && !reservedPackages.contains(packageName)) {
am.restartPackage(packageName);
compteProcessusTues++;
}
}
// Auto-kill (désactivé dans le cas d'un widget)
//am.restartPackage(context.getPackageName());
}
super.onReceive(context, intent);
}
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droideilhan.ultrasimpletaskkiller"
android:versionCode="2"
android:versionName="1.1" >
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="@drawable/icon"
android:label="Google Search" >
<receiver
android:name=".USTaskKillerWidget"
android:label="Google Search" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.droideilhan.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/ustk_widget_provider" />
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RESTART_PACKAGES" >
</uses-permission>
</manifest>
问题 - 小部件的操作永远不会被调用,没有强制关闭,也没有错误。
以下是 TestActivity 中的按钮(这是我到目前为止所尝试过的)
public void test(View view) {
Intent intent = new Intent();
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
sendBroadcast(intent);
intent.setAction("com.droideilhan.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER");
sendBroadcast(intent);
}