AppWidget 的使用之 PendingIntent

這幾天學(xué)習(xí) AppWidget ,很簡單的組件卻花費(fèi)了不少功夫,今天對 PendingIntent 的用法做了一些簡單的整理。

PendingIntent

PandingIntent 就像是一個(gè)設(shè)計(jì)好的處理預(yù)案,當(dāng)達(dá)到某個(gè)特定條件時(shí),便會(huì)調(diào)用該 Intent 所指定動(dòng)作(打開服務(wù),Activity或者發(fā)送廣播)。

這里使用該方法在 AppWidget 里面為按鈕添加監(jiān)聽事件,當(dāng)按鈕被點(diǎn)擊的時(shí)候觸發(fā)相應(yīng)的動(dòng)作

AppWidget 和應(yīng)用程序不在同一個(gè)進(jìn)程當(dāng)中,而是在 HomeScreen 上面執(zhí)行,所以不能直接為 AppWidget 中的 Button 添加監(jiān)聽事件,需要用

remoteViews.setPendingIntent(R.id.widget_button,pendingIntent);

意思是當(dāng)按下按鈕的時(shí)候 pendingIntent 中的 Intent 就會(huì)執(zhí)行

  • PendingIntent 當(dāng)某個(gè)事件出現(xiàn)之后才會(huì)執(zhí)行
  • RemoteViews對象 代表了一系列的 View 對象,和主程序不在同一個(gè)進(jìn)程為 AppWidget 控件綁定處理器

流程概述:

  • 添加 appwidget_provider_info.xml
    在 res/xml 下新建 appwidget_provider_info.xml
    描述 AppWidget 的基本信息如最小高度、寬度等,還有就是該掛件的布局文件
  • 在 res/layout 下面為該掛件設(shè)置具體的布局樣式
    • 向 AppWidget 的布局文件中添加一個(gè) Button
    • 向 AppWidget 的布局文件中添加一個(gè) TextView
  • 新建 MyAppWidget.java 繼承自 AppWidgetProvider
    • 在該類的 onUpdate() 方法中為 Button 設(shè)置、添加監(jiān)聽事件
    • 建立一個(gè) Intent 對象
      用該 Intent 對象創(chuàng)建一個(gè) PendingIntent 對象
    • 創(chuàng)建一個(gè) RemoteViews 對象
      用該 RemoveViews 對象為 按鈕綁定事件處理器
    • 更新按鈕
  • 注冊事件
    備注:要是為 AppWidget 中的 Button 設(shè)置的事件是打開一個(gè) TargetActivity ,還需要添加一個(gè) TargetActivity 類和對應(yīng)的布局文件

代碼

  • appwidget_provider_info.xml
    這個(gè)布局文件是 AppWidget 的信息
    描述了 AppWidget 的最小高,最小寬以及它的布局文件
<appwidget-provider
android:minHeight="200dp"
android:minWidth="300dp"
android:initialLayout="@layout/app_widget"
xmlns:android="http://schemas.android.com/apk/res/android" >
</appwidget-provider>
  • app_widget.xml
    這個(gè)布局文件是 Widget 在桌面上顯示的樣式
    定義了 AppWidget 中各個(gè)組件及其樣式
    其中 Button 用來響應(yīng)點(diǎn)擊事件,加入 TargetActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="hello,world!"/>

<Button
    android:id="@+id/app_widget_btn"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:background="#ff00ff"
    android:text="this is my app widget button"/>
</LinearLayout>
  • target_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="50sp"
    android:background="#00ff00"
    android:text="\n hello,welcome to target activity!"/>

</LinearLayout>
  • MyAppWidget.java
    主要是修改了 update() 方法:
    定義了一個(gè)預(yù)先設(shè)定的動(dòng)作—- Intent 對象;
    利用該 Intent 讀寫,創(chuàng)建一個(gè) PendingIntent 對象;
    創(chuàng)建一個(gè) RemoteView 對象,并為按鈕綁定監(jiān)聽事件
    刷新 AppWidget。
public class MyAppWidget extends AppWidgetProvider {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);
}

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

    //appWidgetIds 每一次向屏幕添加 AppWidget 的時(shí)候都會(huì)增加一個(gè)唯一的 appWidget 的 Id
    for(int i = 0; i < appWidgetIds.length;i++){
      //創(chuàng)建一個(gè) Intent 對象
        Intent intent = new Intent(context,TargetActivity.class);
        //創(chuàng)建一個(gè) PendingIntent 對象
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
        // remoteViews 代表 AppWidget 上所有的控件
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
        //為按鈕綁定事件處理器
        /*
        * 參1,指定被綁定處理器的控件id
        * 參2,指定事件發(fā)生時(shí)會(huì)被執(zhí)行的 PendingIntent
         */
        remoteViews.setOnClickPendingIntent(R.id.app_widget_btn,pendingIntent);
        //更新 AppWidget ,參1是用于指定被更新 appWidget 的ID
        appWidgetManager.updateAppWidget(appWidgetIds[i],remoteViews);
    }
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onDeleted(context, appWidgetIds);
}

@Override
public void onEnabled(Context context) {
    // TODO Auto-generated method stub
    super.onEnabled(context);
}

@Override
public void onDisabled(Context context) {
    // TODO Auto-generated method stub
    super.onDisabled(context);
}
}
  • TargetActivity.java
public class TargetActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.target_activity);
    }
}
  • AndroidManifest.xml
    在 AndroidManifest.xml 中注冊 TargetActivity 和 MyAppWidget
<application>
...
<activity android:name=".TargetActivity">
</activity>

<!-- 注意這里注冊了一個(gè) MyAppWidget 接收數(shù)據(jù)-->
<receiver android:name=".MyAppWidget">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/appwidget_provider_info"/>
</receiver>
</application>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容