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>


在/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>


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. }


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>



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. }

你可能感兴趣的:(android,layout,Class,action,import,encoding)