我想從我的MainActivity傳遞一個ArrayList到CheckRunningApplicationReceiver.class(廣播接收器)。 CheckRunningApplicationReceiver.class從另一個廣播接收器調用,稱爲StartupReceiver,每5秒鐘由警報管理器調用。我是否需要先將ArrayList傳遞給StartupReceiver,然後再從那裏傳遞給CheckRunningApplicationReceiver?或者有什麼辦法可以直接從MainActivity傳遞給CheckRunningApplicationReceiver?如何將Arraylist從一個活動傳遞到另一個廣播接收器的廣播接收器
StartupReceiver.class
public class StartupReceiver extends BroadcastReceiver {
static final String TAG = "SR";
final int startupID = 1111111;
@Override
public void onReceive(Context context, Intent intent) {
// Create AlarmManager from System Services
final AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
try{
// Create pending intent for CheckRunningApplicationReceiver.class
// it will call after each 5 seconds
Intent i7 = new Intent(context, CheckRunningApplicationReceiver.class);
PendingIntent ServiceManagementIntent = PendingIntent.getBroadcast(context,
startupID, i7, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime(),
1000, ServiceManagementIntent);
} catch (Exception e) {
Log.i(TAG, "Exception : "+e);
}
}
}
CheckRunningApplicationReceiver.class 公共類CheckRunningApplicationReceiver延伸的BroadcastReceiver {
public final String TAG = "CRAR"; // CheckRunningApplicationReceiver
字符串程序包; ConnectivityManager dataManager; ConnectivityManager dataManager;
@Override
public void onReceive(Context aContext, Intent anIntent) {
dataManager = (ConnectivityManager)aContext.getSystemService(aContext.CONNECTIVITY_SERVICE);
Method dataMtd = null;
try {
dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dataMtd.setAccessible(true);
try {
// Using ACTIVITY_SERVICE with getSystemService(String)
// to retrieve a ActivityManager for interacting with the global system state.
ActivityManager am = (ActivityManager) aContext
.getSystemService(Context.ACTIVITY_SERVICE);
// Return a list of the tasks that are currently running,
// with the most recent being first and older ones after in order.
// Taken 1 inside getRunningTasks method means want to take only
// top activity from stack and forgot the olders.
List<ActivityManager.RunningTaskInfo> alltasks = am
.getRunningTasks(1);
//
for (ActivityManager.RunningTaskInfo aTask : alltasks) {
// When user on call screen show a alert message
if (aTask.topActivity.getClassName().equals("com.android.mms.ui.ConversationList")
|| aTask.topActivity.getClassName().equals("com.android.mms.ui.ComposeMessageActivity"))
{
// When user on Send SMS screen show a alert message
try {
dataMtd.invoke(dataManager, false);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (Throwable t) {
Log.i(TAG, "Throwable caught: "
+ t.getMessage(), t);
}
}
MainActivity.class
@Override
protected void onResume()
{
// TODO Auto-generated method stub
getBaseContext().getApplicationContext().sendBroadcast(
new Intent("StartupReceiver_Manual_Start"));//to start StartupReceiver
super.onResume();
}
清單
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.javatechig.listapps.AllAppsActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.javatechig.listapps.Adblock"
android:label="@string/app_name" >
</activity>
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="StartupReceiver_Manual_Start" />
</intent-filter>
</receiver>
<receiver android:name = ".CheckRunningApplicationReceiver"/>
</application>
謝謝你的答案,但我使用共享偏好從主要活動保存了數組列表,並直接從第二個廣播接收器中檢索它。 –