appwidget onupdate没有被调用以及按钮点击一段时间后不工作

时间:2014-05-30 04:16:30

标签: android xml android-appwidget android-pendingintent appwidgetprovider

我有一个简单的小部件,它有三个按钮,用于三种不同的系统操作。

我在xml文件夹中创建了一个XML,代码如下。

<appwidget-provider 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:minWidth="292dp" 
android:updatePeriodMillis="86400000" 
android:minHeight="32dp"     
android:initialLayout="@layout/activity_main">

我的布局文件位于

之下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#D8F781"
>
<Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true"
  android:text="Off" />
  <Button android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@+id/button1"
  android:text="off"/>

 <Button android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/button2"
 android:text="off"/>   
</RelativeLayout>

以下是扩展AppWidgetProvider的MainActivity代码

public class MainActivity extends AppWidgetProvider{
Context ctx;
private static final String XYZ    = "xyz";
private static final String ABC    = "abc";
private static final String CFV    = "cfv";


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
  ctx = context;
  for(int i=0; i<appWidgetIds.length; i++){
  int currentWidgetId = appWidgetIds[i];
  RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.activity_main);     

  views.setOnClickPendingIntent(R.id.button2, getPendingSelfIntent(context, XYZ));
  views.setOnClickPendingIntent(R.id.button1, getPendingSelfIntent(context, ABC));
  views.setOnClickPendingIntent(R.id.button3, getPendingSelfIntent(context, CFV));
  appWidgetManager.updateAppWidget(currentWidgetId,views);      
  Toast.makeText(context, "widget used", Toast.LENGTH_SHORT).show();    
  }
 }

protected PendingIntent getPendingSelfIntent(Context context, String action) {
   Log.v("ONMESSAGE", action);
   Intent intent = new Intent(context, getClass());
   intent.setAction(action);
   return PendingIntent.getBroadcast(context, 0, intent, 0);
}
@Override
public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   super.onReceive(context, intent);
   ctx = context;
   final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
   RemoteViews remoteViews;
   AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);       
   ComponentName watchWidget;
   remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
   if (XYZ.equals(intent.getAction())) {  
       //Logic for button 2
   }
   else if(ABC.equals(intent.getAction()))
   {      
      //Logic for button 1
   }
   else if(AUTO_ROTATE.equals(intent.getAction()))
   {      
      //Logic for button 3
   }


   watchWidget = new ComponentName( context, MainActivity.class );
   (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
 }
 @Override
public void onEnabled(Context context) {
    // TODO Auto-generated method stub
    super.onEnabled(context);
    Log.v("ONMESSAGE", "Working");
}

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.afixi.tetheringwidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="MainActivity" >
        <intent-filter>
           <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
           android:resource="@xml/main" />
     </receiver>
</application>

首次安装时,一切正常,但是在我更改代码然后重新安装后,现在它无法正常工作。我是第一次制作小部件。

N:B: - 只是尝试在新手机中安装它,现在它正在运行,我想一段时间后,它要么没有更新,要么停止从小部件接收待处理的意图!

0 个答案:

没有答案