2013-05-07 93 views
0

我有一個小部件,其中包含TextView,我只想在Onreceive() {此接收機不是爲小部件實現的小部件}方法被調用時更新。我不想定期更新小部件,但只有當我的Onreceive()被調用時。非週期性地更新小部件

Recver.java

public class Recver extends BroadcastReceiver { 



    @Override 
    public void onReceive(Context context, Intent intent) { 

       Widgetd.updatewid(context); 

      } 
    } 

    }} 

Widgetd.java

public class Widgetd extends AppWidgetProvider { 


    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
      int[] appWidgetIds) { 
     // TODO Auto-generated method stub 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 

    } 

    @Override 
    public void onDeleted(Context context, int[] appWidgetIds) { 
     // TODO Auto-generated method stub 
     super.onDeleted(context, appWidgetIds); 
    } 
    static void updatewid(Context context){ 

     RemoteViews rv = new RemoteViews(context.getPackageName(), 
       R.layout.widgetlay); 
     rv.setTextViewText(R.id.widgetUpdateTv, "1"); 



    } 

} 

的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="delete.detailduplicate" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/> 
    <uses-permission android:name="android.permission.READ_SMS"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="delete.detailduplicate.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="Newmain"></activity> 
     <receiver android:name="Recver"> 
      <intent-filter android:priority="9999999"> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
    </intent-filter> 
     </receiver> 
     <receiver android:name="Widgetd"> 
      <intent-filter > 
       <action 
        android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/widgetsetter" /> 
     </receiver> 
    </application> 

</manifest> 

widgetsetter.xml

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialLayout="@layout/widgetlay" 
    android:minHeight="300dp" 
    android:minWidth="72dp" 
    android:updatePeriodMillis="0" > 

</appwidget-provider> 

回答

2

設置updatePeriodMillis = "0"

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
android:initialLayout="@layout/widget_initial_layout" 
android:minHeight="40dp" 
android:minWidth="40dp" 
android:updatePeriodMillis="0" 
android:widgetCategory="home_screen" 
android:previewImage="@drawable/my_app_logo"/> 

添加應觸發更新的AndroidManifest(這裏例如WIFI_STATE_CHANGED)的意圖:

 <intent-filter> 
      <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 
     </intent-filter> 

和應用Widget代碼調用你的updateWidget方法:

@Override 
    public void onReceive(Context context, Intent intent) 
    { 
      Log.d(TAG, "onReceive() " + intent.getAction()); 
      super.onReceive(context, intent); 

      if (intent != null) 
      { 
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) 
        { 
          updateWidgets(context); 
        } 
      ... 
      } 
    } 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    { 
     Log.d(TAG, "onUpdate()"); 
     updateWidgets(context); 
    } 


    private void updateWidgets(Context context) 
    { 
     AppWidgetManager m = AppWidgetManager.getInstance(context); 
     if (m != null) 
     { 
      int[] appWidgetIds = 
        m.getAppWidgetIds(new ComponentName(context, getClass())); 
      if ((appWidgetIds != null) && (appWidgetIds.length > 0)) 
      { 
       final RemoteViews updateViews = 
        new RemoteViews(context.getPackageName(), 
        R.layout.mywidget_layout); 
        ... 
        m.updateAppWidget(appWidgetIds, updateViews); 
      } 
     } 
    } 
+0

它不需要註冊擴展AppWidgetProvider的類來註冊爲清單 – dreamer1989 2013-05-07 23:14:18

+0

中的接收者,如果我沒有按照註冊接收器的常規方法,小部件不會創建。如上所述,我在類中創建了一個方法,擴展AppWidgetProvider並從Onreceive()方法調用它,但它不更新小部件。 – dreamer1989 2013-05-07 23:58:10

+0

顯示一些代碼! – 2013-05-08 05:07:34