我想从我的MainActivity将一个arraylist传递给CheckRunningApplicationReceiver.class(广播接收器)。 CheckRunningApplicationReceiver.class是从另一个名为StartupReceiver的广播Receiver中调用的,它每隔5秒由一个警报管理器调用。我是否需要先将arraylist传递给StartupReceiver,然后再从那里传递给CheckRunningApplicationReceiver?或者有没有办法直接从MainActivity传递给CheckRunningApplicationReceiver?
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
String package_name; 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>
答案 0 :(得分:0)
使用putParcelableArrayListExtra()
将列表放入Intent
和getParcelableArrayListExtra()
以获取Intent
的列表。
是的,您需要将其从Activity
传递到第一个BroadcastReceiver
,然后将其从传入的Intent
复制到用于传递的Intent
触发第二个BroadcastReceiver
。
答案 1 :(得分:0)
我所做的是用户选择特定的应用程序,因此我将这些选定的应用程序存储在arraylist中并使用共享首选项存储它。使用报警管理器每1秒调用一次启动广播接收器,该报警管理器调用checkrunningapps广播接收器。在这里,我检索存储的arraylist并将其与当前的前景应用程序进行比较,并做我想做的任何事情,如应用程序锁和东西。但问题是它每秒都检索到arraylist并检查它。因为这个,应用程序有点滞后。我是android的新手。我认为我的方法是错误的。我也尝试使用服务,但在服务onstart命令中代码只执行一次,因此它只检查当前正在运行的应用程序与arraylist一次,即使该服务无限期地处于后台。请告诉我使用后台服务的正确方法。请给我发送代码。我经常搜索但没有运气。我希望该服务只获取arraylist一次,并将其与当前正在运行的应用程序永远进行比较。