現在模擬一下有三支APK,分別A1丟PendingIntent給A2,A2依接受到的PendingIntent而執行A3。因為 PendingIntent的機制,所以A2是動態決定要執行誰的,如果今天有另一支APK丟了指定要執行Broadcast的 PendingIntent給A2時,那A2就會執行廣播等待系統某一個Receiver去接受這個廣播。程式碼如下:
public class A1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.action.a3");
intent.putExtra("Data", "Information from PendingIntent");
PendingIntent mPendingIntent = PendingIntent.getActivity(
A1.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent("com.action.a2");
intent2.putExtra("Data", "Information from A1");
// 因為PendingIntent類別有implements Parcelable,
// 所以可以直接用puxExtra傳遞
intent2.putExtra("myPendingIntent", mPendingIntent);
startActivity(intent2);
}
};
Button mButton = (Button) findViewById(R.id.Button01);
mButton.setOnClickListener(mListener);
}
}
AndroidManifest.xml如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a1" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".A1" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
public class A2 extends Activity {
public Intent mIntent;
public PendingIntent mPendingIntent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntent = getIntent();
TextView txt = (TextView) findViewById(R.id.txt1);
txt.setText(mIntent.getStringExtra("Data"));
mPendingIntent = mIntent.getParcelableExtra("myPendingIntent");
OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
if (mPendingIntent != null) {
try {
mPendingIntent.send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
}
};
Button mButton = (Button) findViewById(R.id.Button01);
mButton.setOnClickListener(mListener);
}
}
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a2" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".A2" android:label="@string/app_name">
<intent-filter>
<action android:name="com.action.a2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
public class A3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent mIntent = getIntent();
TextView txt = (TextView) findViewById(R.id.txt1);
txt.setText(mIntent.getStringExtra("Data"));
}
}
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a3" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".A3" android:label="@string/app_name">
<intent-filter>
<action android:name="com.action.a3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
執行畫面如下:
public class A1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.action.a3");
intent.putExtra("Data", "Information from PendingIntent");
PendingIntent mPendingIntent = PendingIntent.getActivity(
A1.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent("com.action.a2");
intent2.putExtra("Data", "Information from A1");
// 因為PendingIntent類別有implements Parcelable,
// 所以可以直接用puxExtra傳遞
intent2.putExtra("myPendingIntent", mPendingIntent);
startActivity(intent2);
}
};
Button mButton = (Button) findViewById(R.id.Button01);
mButton.setOnClickListener(mListener);
}
}
AndroidManifest.xml如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a1" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".A1" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
public class A2 extends Activity {
public Intent mIntent;
public PendingIntent mPendingIntent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntent = getIntent();
TextView txt = (TextView) findViewById(R.id.txt1);
txt.setText(mIntent.getStringExtra("Data"));
mPendingIntent = mIntent.getParcelableExtra("myPendingIntent");
OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
if (mPendingIntent != null) {
try {
mPendingIntent.send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
}
};
Button mButton = (Button) findViewById(R.id.Button01);
mButton.setOnClickListener(mListener);
}
}
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a2" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".A2" android:label="@string/app_name">
<intent-filter>
<action android:name="com.action.a2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
public class A3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent mIntent = getIntent();
TextView txt = (TextView) findViewById(R.id.txt1);
txt.setText(mIntent.getStringExtra("Data"));
}
}
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a3" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".A3" android:label="@string/app_name">
<intent-filter>
<action android:name="com.action.a3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
執行畫面如下:
沒有留言:
張貼留言