2015-04-06 108 views
1

我想從我的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> 

回答

0

我做的是用戶選擇特定的應用程序,所以我將這些選定的應用程序存儲在arraylist中並使用共享首選項進行存儲。啓動廣播接收器每1秒鐘使用一個叫做檢查廣播接收器的報警管理器調用。在這裏我檢索了存儲的數組列表,並將其與當前前臺應用程序進行比較,並執行我想要的任何操作,如應用程序鎖定和其他內容但問題是它每秒鐘都會檢索數組列表並檢查它。由於這個原因,應用程序有點滯後。我是android新手。我認爲我的方法是錯誤的。我也嘗試使用服務,但是在服務onstart命令中,代碼只執行一次,所以即使服務無限期地處於後臺,它也僅使用ArrayList檢查當前正在運行的應用程序。請告訴我使用後臺服務的正確方法。請給我發送代碼。我hv搜索了很多,但沒有運氣。我希望該服務只讀取一次數組列表,並將其與目前運行的應用程序的永久前景進行比較。

0

使用putParcelableArrayListExtra()放列表進入IntentgetParcelableArrayListExtra()獲得從Intent列表。

是的,你將需要從Activity它傳遞給第一BroadcastReceiver,它可以將其從輸入Intent複製到用於觸發第二BroadcastReceiver傳出Intent

+0

謝謝你的答案,但我使用共享偏好從主要活動保存了數組列表,並直接從第二個廣播接收器中檢索它。 –