繁体   English   中英

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

[英]How to pass Arraylist from an activity to Broadcast receiver to another broadcast receiver

我想将数组列表从MainActivity传递给CheckRunningApplicationReceiver.class(广播接收器)。 从另一个广播接收器StartupReceiver调用CheckRunningApplicationReceiver.class。警报接收器每5秒钟调用一次广播接收器StartupReceiver。我是否需要先将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

字符串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>

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM