Widget中PendingIntent和RemoteViews用法

PendingIntent是指把Intent包装了一层, 并且把PendingIntent放入一个新的进程. 通过触发事件去触发这个PendingIntent.

创建PendingIntent方法.

1. getActivity(Context context, int requestCode, Intent intent, int flags)

2. getBroadcast(Context context, int requestCode, Intent intent, int flags)

3. getService(Context context, int requestCode, Intent intent, int flags)

例子:
在/res/xml中新建一个example_appwidget.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:minWidth="294dp" android:minHeight="72dp"
  4. android:updatePeriodMillis="86400000" android:initialLayout="@layout/example_appwidget_info">
  5. </appwidget-provider>
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/example_appwidget_info"> </appwidget-provider>

在/res/layout中新建一个example_widget.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView android:text="firstWidgetText" android:background="#000000"
  6. android:id="@+id/widgetTextId" android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"></TextView>
  8. <Button android:layout_width="match_parent" android:id="@+id/button1" android:text="Button" android:layout_height="wrap_content"></Button>
  9. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="firstWidgetText" android:background="#000000" android:id="@+id/widgetTextId" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView> <Button android:layout_width="match_parent" android:id="@+id/button1" android:text="Button" android:layout_height="wrap_content"></Button> </LinearLayout>

main.xml的内容不变

ExampleAppWidgetProvider.java内容

Java代码 复制代码 收藏代码
  1. package com.cn;
  2. import android.app.PendingIntent;
  3. import android.appwidget.AppWidgetManager;
  4. import android.appwidget.AppWidgetProvider;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.widget.RemoteViews;
  8. public class ExampleAppWidgetProvider extends AppWidgetProvider {
  9. @Override
  10. public void onDeleted(Context context, int[] appWidgetIds) {
  11. System.out.println("onDeleted");
  12. super.onDeleted(context, appWidgetIds);
  13. }
  14. @Override
  15. public void onDisabled(Context context) {
  16. System.out.println("onDisabled");
  17. super.onDisabled(context);
  18. }
  19. @Override
  20. public void onEnabled(Context context) {
  21. System.out.println("onEnabled");
  22. super.onEnabled(context);
  23. }
  24. @Override
  25. public void onReceive(Context context, Intent intent) {
  26. System.out.println("onReceive");
  27. super.onReceive(context, intent);
  28. }
  29. @Override
  30. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  31. int[] appWidgetIds) {
  32. System.out.println("onUpdate");
  33. for (int i = 0; i < appWidgetIds.length; i++) {
  34. System.out.println(appWidgetIds[i]);
  35. //创建一个intent对象
  36. Intent intent = new Intent(context,TargetActivity.class);
  37. //创建一个PendingIntent
  38. PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
  39. RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_widget);
  40. //第一个参数用来指定绑定事件的id
  41. //第二个参数指定事件发生时, 哪个pendingIntent会被执行
  42. remoteViews.setOnClickPendingIntent(R.id.button1, pending);
  43. //第一个参数是更新哪个appwidget的id
  44. //第二个是传remoteView
  45. appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
  46. }
  47. super.onUpdate(context, appWidgetManager, appWidgetIds);
  48. }
  49. }
package com.cn; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; public class ExampleAppWidgetProvider extends AppWidgetProvider { @Override public void onDeleted(Context context, int[] appWidgetIds) { System.out.println("onDeleted"); super.onDeleted(context, appWidgetIds); } @Override public void onDisabled(Context context) { System.out.println("onDisabled"); super.onDisabled(context); } @Override public void onEnabled(Context context) { System.out.println("onEnabled"); super.onEnabled(context); } @Override public void onReceive(Context context, Intent intent) { System.out.println("onReceive"); super.onReceive(context, intent); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { System.out.println("onUpdate"); for (int i = 0; i < appWidgetIds.length; i++) { System.out.println(appWidgetIds[i]); //创建一个intent对象 Intent intent = new Intent(context,TargetActivity.class); //创建一个PendingIntent PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0); RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_widget); //第一个参数用来指定绑定事件的id //第二个参数指定事件发生时, 哪个pendingIntent会被执行 remoteViews.setOnClickPendingIntent(R.id.button1, pending); //第一个参数是更新哪个appwidget的id //第二个是传remoteView appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); } super.onUpdate(context, appWidgetManager, appWidgetIds); } }

AndroidManifest.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cn"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="10" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".Appwidget2Activity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. <receiver android:name="ExampleAppWidgetProvider">
  16. <!-- 指定广播接收 -->
  17. <intent-filter>
  18. <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
  19. </intent-filter>
  20. <!-- appwidget的源文件 -->
  21. <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info"></meta-data>
  22. </receiver>
  23. <activity android:name="TargetActivity"></activity>
  24. </application>
  25. </manifest>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Appwidget2Activity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="ExampleAppWidgetProvider"> <!-- 指定广播接收 --> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action> </intent-filter> <!-- appwidget的源文件 --> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info"></meta-data> </receiver> <activity android:name="TargetActivity"></activity> </application> </manifest>


TargetActivity.java内容
Java代码 复制代码 收藏代码
  1. package com.cn;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class TargetActivity extends Activity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.main);
  9. }
  10. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值