Android:我的BOOTCOMPLETED接收课程没有启动我的闹钟

时间:2016-07-28 03:36:22

标签: android broadcastreceiver alarmmanager

我遇到了一个我无法弄清楚的问题。我有一个重复的警报广播接收器,我已经设置了另一个广播接收器来接收BOOT_COMPLETE信号,以便在启动时重新启动我的警报。代码看起来像我查找过的其他人,所以我不确定是什么问题。

我的启动接收器:

public class DeviceBootReceiver extends BroadcastReceiver {
Context context2;

public boolean fileExistance(String fname){
    File file = context2.getApplicationContext().getFileStreamPath(fname);
    return file.exists();
}




public String loadCurrentWhat (String filename){
    FileInputStream fileInputStream;
    String info = "";
    try {
        fileInputStream = context2.getApplicationContext().openFileInput(filename);
        int count;
        while ((count=fileInputStream.read()) != -1){
            info += Character.toString((char)count);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return info;
}

@Override
public void onReceive(Context context, Intent intent) {
    context2 = context;
        if (fileExistance("number")){
                 /* Setting the alarm here */
                Intent alarmIntent = new Intent(context, AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

                AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                int interval = 10000;
                manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
        }

}
}

我的重复闹钟:

public class AlarmReceiver extends BroadcastReceiver {


Context context2;

@Override
public void onReceive(Context context, Intent intent) {

    // For our recurring task, we update the character data in internal storage.
    context2 = context.getApplicationContext();
    UPDATE();

}
}

我的宣言:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".CardFullView"/>

    <receiver
        android:name=".DeviceBootReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <receiver
        android:name=".AlarmReceiver">
    </receiver>



</application>

1 个答案:

答案 0 :(得分:1)

从目前为止我能看到的内容,您需要将RECEIVE_BOOT_COMPLETED权限 置于 application标记。

试试这个。这应该有用。