android中的appwidget理解鞏固

來源:互聯網
上載者:User

public class MyAppWidget extends AppWidgetProvider{

private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("-------------onDeleted----------------");
super.onDeleted(context, appWidgetIds);
}

//當最後一個appwidget刪除後
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println("-------------onDisabled----------------");
super.onDisabled(context);
}

//當第一次被建立時
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println("-------------onEnabled----------------");
super.onEnabled(context);
}

//接收廣播
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

String actionString = intent.getAction();
System.out.println("-------------onReceive----------------"+actionString);
if(CLICK_ACTION.equals(actionString)){
System.out.println("===============");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
remoteViews.setTextViewText(R.id.text01, context.getResources().getString(R.string.hello_world));
remoteViews.setViewVisibility(R.id.button01, View.INVISIBLE);
Toast.makeText(context, "success", Toast.LENGTH_SHORT).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, MyAppWidget.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}else{
super.onReceive(context, intent);
}

}

//用於更新操作
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("-------------onUpdate----------------");

Intent intent = new Intent();
intent.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
rViews.setOnClickPendingIntent(R.id.button01, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, rViews);

}

}

在manifest檔案裡面

 <receiver android:name="MyAppWidget" android:exported="false">
            <!-- 所需要滿足的過濾器 -->
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <intent-filter>
                <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
            </intent-filter>
             <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/appwidget01_info" />
        </receiver>

學了這麼久,總結點appwidget的自己理解的東西記錄下來!

執行順序:

首先-->在案頭建立appwidget案頭小程式,

重點是  <intent-filter>
                <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
            </intent-filter>

和代碼裡的Intent intent = new Intent();
intent.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);和String actionString = intent.getAction();

intent.setAction(CLICK_ACTION);是將private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";設定action來執行

然後和manifest裡的 <intent-filter>
                <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
            </intent-filter>比對,如果相匹配就為pendingIntent對象的廣播事件設定這個action,所以在onReceive裡面就用String actionString = intent.getAction();得到

在manifest匹配的action對象

還有種比較簡單的使用getActivity(Context context, int requestCode, Intent intent, int flags);  建立PendingIntent對象

其中的intent對象是通過Intent  intent = new Intent(context,XXX.class);來建立的,可以開啟一個activity

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.