AppWidget 的使用之 PendingIntent

2017-08-26  本文已影响108人  s0me0ne

这几天学习 AppWidget ,很简单的组件却花费了不少功夫,今天对 PendingIntent 的用法做了一些简单的整理。

PendingIntent

PandingIntent 就像是一个设计好的处理预案,当达到某个特定条件时,便会调用该 Intent 所指定动作(打开服务,Activity或者发送广播)。

这里使用该方法在 AppWidget 里面为按钮添加监听事件,当按钮被点击的时候触发相应的动作

AppWidget 和应用程序不在同一个进程当中,而是在 HomeScreen 上面执行,所以不能直接为 AppWidget 中的 Button 添加监听事件,需要用

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

意思是当按下按钮的时候 pendingIntent 中的 Intent 就会执行

流程概述:

代码

<appwidget-provider
android:minHeight="200dp"
android:minWidth="300dp"
android:initialLayout="@layout/app_widget"
xmlns:android="http://schemas.android.com/apk/res/android" >
</appwidget-provider>
<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>
<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>
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 的时候都会增加一个唯一的 appWidget 的 Id
    for(int i = 0; i < appWidgetIds.length;i++){
      //创建一个 Intent 对象
        Intent intent = new Intent(context,TargetActivity.class);
        //创建一个 PendingIntent 对象
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
        // remoteViews 代表 AppWidget 上所有的控件
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
        //为按钮绑定事件处理器
        /*
        * 参1,指定被绑定处理器的控件id
        * 参2,指定事件发生时会被执行的 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);
}
}
public class TargetActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.target_activity);
    }
}
<application>
...
<activity android:name=".TargetActivity">
</activity>

<!-- 注意这里注册了一个 MyAppWidget 接收数据-->
<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>
上一篇 下一篇

猜你喜欢

热点阅读