如何将 Arraylist 从 activity 传递给广播接收器到另一个广播接收器

How to pass Arraylist from an activity to Broadcast receiver to another broadcast receiver

我想将数组列表从我的 MainActivity 传递给 CheckRunningApplicationReceiver.class(广播接收器)。 CheckRunningApplicationReceiver.class 是从另一个名为 StartupReceiver 的广播接收器调用的,它由警报管理器每 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 public class CheckRunningApplicationReceiver 扩展 BroadcastReceiver {

public final String TAG = "CRAR"; // CheckRunningApplicationReceiver

字符串package_name; ConnectivityManager 数据管理器;

@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>

使用 putParcelableArrayListExtra() 将列表放入 Intent 并使用 getParcelableArrayListExtra()Intent 获取列表。

是的,您需要将其从 Activity 传递到第一个 BroadcastReceiver,然后它可以将其从传入 Intent 复制到传出 Intent用于触发第二个 BroadcastReceiver.

我所做的是用户选择特定的应用程序,所以我将这些选定的应用程序存储在 arraylist 中并使用共享首选项存储它。使用警报管理器每 1 秒调用一次启动广播接收器,然后调用 check运行apps 广播接收器。在这里,我检索了存储的数组列表并将其与当前的前台应用程序进行比较,然后做任何我想做的事情,比如应用程序锁和其他东西。但问题是它每秒检索 arraylist 并检查它。因此,应用程序有点滞后。我是 android 的新手。我认为我的方法是错误的。我也尝试使用服务,但在服务 onstart 命令中,代码只执行一次,所以它只检查当前 运行 应用程序与 arraylist 一次,即使服务无限期地在后台。请告诉我使用后台服务的正确方法。请把代码发给我。我搜索了很多但没有运气。我希望该服务仅获取一次数组列表并将其与当前前台 运行 应用程序永远进行比较。