편집 : 재부팅 후 알람이 울리면 관리자가 런타임이 아닌 매니페스트에 등록해야합니다.재부팅 후 안드로이드 알람이 작동하지 않습니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testrtc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.testrtc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SampleBootReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
: 여기 public class SampleBootReceiver extends BroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
BroadcastReceiver br;
TextView t;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "Hello from Bootloader", 10000).show();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 22); // Particular minute
calendar.set(Calendar.SECOND, 0);
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000*60*60*24, alarmIntent);
}
}
}
내 매니페스트입니다 : 내가하는 SampleBootReceiver 클래스가 public class MainActivity extends Activity {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
BroadcastReceiver br;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setup();
t = (TextView)findViewById(R.id.textView1);
ComponentName receiver = new ComponentName(getApplicationContext(), SampleBootReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 22); // Particular minute
calendar.set(Calendar.SECOND, 0);
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000*60*60*24, alarmIntent);
}
public void setup() {
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
//Invoke the service here Put the wake lock and initiate bind service
t.setText("Hello Alarm set");
}
};
registerReceiver(br, new IntentFilter("com.testrtc"));
alarmIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.testrtc"),
0);
alarmMgr = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
:
나는 Here에서 사용 지침을 디자인 한 다음 클래스가
알람이 정상적으로 작동하고 있습니다. 재부팅 후 BootReceiver 클래스에서 축배 메시지를받습니다. 그러나 알람은 재설정되지 않습니다. 앱에서 사용자가 적어도 한 번 이상 시작하지 않으면 알람이 재설정되지 않는다고 나와있는 문서 도구 상태에서 하나의 지점을 분명히하고 싶습니다. Set the RECEIVE_BOOT_COMPLETED permission in your application's manifest. This allows your app to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting (this only works if the app has already been launched by the user at least once):
이 문장의 컨텍스트는 무엇입니까? 사용자가 앱을 다시 시작해야하는 경우 어쨌든 onCreate가 호출되고 알람이 다시 설정됩니다. 또는이 말은 수명주기 동안 앱이 적어도 한 번은 휴대 전화를 사용해야한다는 의미입니까?
를 얻을 수
SampleBootReceiver
다시이 줄을 추가 .. ?? – SilentKillerAPI 레벨 19에서 테스트하지 않았지만이 문제는 Jelly Bean에 있습니다. – Skynet
JellyBean에서 똑같은 문제가 발생했다는 생각이 들었고 BroadcastListener가 부팅 후 자동으로 4.0 이상에서 작동하지 않아 Broadcast를 실행하는 서비스를 만들었습니다. – SilentKiller