我创建了一个 keyguard 小部件,它有一个按钮,在单击时从 SharedPreferences 更新 textview。它在主屏幕上正常工作,但在锁定屏幕(android 4.2.2)上它(s按钮)仅在我将其添加到我的锁定屏幕小部件但当我关闭屏幕并返回时工作;它(s按钮)不起作用!
小部件.java
public class Widget extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
for(int i=0;i<appWidgetIds.length;i++){
long l = System.currentTimeMillis();
Intent intent=new Intent(context, Widget_Resiver.class);
intent.setAction("START_WIDGET_SERVICE");
PendingIntent localPendingIntent = PendingIntent.getBroadcast(context, 12, intent, PendingIntent.FLAG_UPDATE_CURRENT);
((AlarmManager)context.getSystemService("alarm")).setRepeating(1, l, 120000L, localPendingIntent);
}
}
}
Widget_Service.java
public class Widget_Service extends Service{
public static String ACTION_WIDGET_BUTTON = "ACTION_WIDGET_BUTTON";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
ComponentName thisWidget = new ComponentName(getApplicationContext(),Widget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
RemoteViews views = new RemoteViews(this.getApplicationContext().getPackageName(),R.layout.widget);
SharedPreferences sP= PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
views.setTextViewText(R.id.widget_textView, String.valueOf(sP.getInt("updateTextView", 0)));
Intent button = new Intent(this, Widget_Resiver.class);
button.setAction(ACTION_WIDGET_BUTTON);
PendingIntent actionPendingIntentB2 = PendingIntent.getBroadcast(this, 0, button, 0);
views.setOnClickPendingIntent(R.id.widget_button, actionPendingIntentB2);
appWidgetManager.updateAppWidget(allWidgetIds, views);
}
}
Widget_Resiver.java
public class Widget_Resiver extends BroadcastReceiver{
public static String START_WIDGET_SERVICE="START_WIDGET_SERVICE";
public static String ACTION_WIDGET_BUTTON = "ACTION_WIDGET_BUTTON";
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
ComponentName thisWidget = new ComponentName(context.getApplicationContext(),Widget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
RemoteViews remoteViews = new RemoteViews(context.getApplicationContext().getPackageName(),R.layout.widget);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor ed = settings.edit();
if(intent.getAction().equals(START_WIDGET_SERVICE)){
context.startService(new Intent(context, Widget_Service.class));
}else if (intent.getAction().equals(ACTION_WIDGET_BUTTON)) {
ed.putInt("updateTextView", settings.getInt("updateTextView", 0)+1);
ed.commit();
remoteViews.setTextViewText(R.id.widget_textView,String.valueOf(settings.getInt("updateTextView", 0)));
appWidgetManager.updateAppWidget(allWidgetIds, remoteViews);
}
}
}
AndroidManifest.xml
<receiver android:name=".Widget" android:label="widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>
<service android:name=".Widget_Service"/>
<receiver android:name=".Widget_Resiver">
<intent-filter>
<action android:name="ACTION_WIDGET_BUTTON"/>
</intent-filter>
</receiver>