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 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>
<?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 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>
<?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内容
- 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 = new Intent(context,TargetActivity.class);
- PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_widget);
-
- remoteViews.setOnClickPendingIntent(R.id.button1, pending);
- appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
- }
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- }
- }
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 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>
-
- <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info"></meta-data>
- </receiver>
- <activity android:name="TargetActivity"></activity>
- </application>
- </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内容
- package com.cn;
- import android.app.Activity;
- import android.os.Bundle;
- public class TargetActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }