转移到SD卡触发EXTERNAL_APPLICATIONS_AVAILABLE广播接收器?

时间:2012-06-17 04:36:32

标签: android android-intent broadcastreceiver

当我去管理应用程序并尝试将任何应用程序移动到SD卡时,它会激活我的广播接收器EXTERNAL_APPLICATIONS_AVAILABLE和BOOT_COMPLETED。

这是我的广播接收器:

    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;

    import java.util.Timer;
    import java.util.TimerTask;

    public class AutoStart extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
            if(preferences.getBoolean(context.getString(R.string.autoStartKey), false))
            {
                AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                PendingIntent pendingIntent = PendingIntent.getService(context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 2, 2, pendingIntent);
                boolean showNotification = preferences.getBoolean(context.getString(R.string.showNotificationKey), false);
                if(showNotification)
                    Preferences.setNotification(context);
            }
            Timer timer = new Timer();
            final Context contextTimer = context;
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Intent i = new Intent(contextTimer, StartupCleaner.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    contextTimer.startActivity(i);
                }
            }, 10000);
        }

}

这是我的清单:

<receiver android:enabled="true" android:name=".AutoStart"
                  android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        <receiver android:enabled="true" android:name=".AutoStart"
                  android:permission="android.permission.EXTERNAL_APPLICATIONS_AVAILABLE">
            <intent-filter>
                <action android:name="android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

0 个答案:

没有答案