我試圖根據手冊創建一些簡單BroadcastReceiver
,但我不能使它工作。 我打電話的過程setAlarm在MainActivity onCreate
功能,看起來像這樣:BroadcastReceiver不工作
private void setAlarm() {
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, pendingIntent);
}
應該激活AlarmReceiver
應該做一些動作,但不會做任何事情。它看起來像這樣:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I'm running", Toast.LENGTH_LONG).show();
notifikace(context);
}
public void notifikace(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.notifikace);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("aaa");
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, mBuilder.build());
}
}
我懷疑我在AndroidManifest中丟失了一些東西,但我不知道是什麼。 AndroidManifest看起來是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Nastaveni"
android:label="@string/title_activity_nastaveni"
android:theme="@style/AppTheme.NoActionBar" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".Tyden"
android:label="@string/title_activity_tyden"
android:theme="@style/AppTheme.NoActionBar"></activity>
<receiver android:name=".AlarmReceiver" >
</receiver>
</application>
最終appliacation應該能夠在每天的特定時間發送通知,即使應用程序沒有在時刻運行。 (我必須再更換setRepeating功能
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, pendingIntent);
線,但到目前爲止,出於測試目的,我需要先讓它運行。) 但到目前爲止AlarmReceiver類並沒有做任何事情。我得到了一些錯誤,當我嘗試運行它(但應用程序不會崩潰):
04-14 11:52:54.592 1300-1354/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.xxx.myapplication3/com.example.xxx.myapplication.MainActivity (has extras)} from uid 10014 on display 0
04-14 11:52:54.673 1300-1635/? I/ActivityManager: START u0 {act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.android.packageinstaller cmp=com.android.packageinstaller/.permission.ui.GrantPermissionsActivity (has extras)} from uid 10057 on display 0
我不知道錯誤日誌是否與此有關什麼的其他項目。我究竟做錯了什麼?預先感謝任何答案。
格式代碼/ XML請 –
難道我的回答解決問題了嗎?如果是這樣,請標記爲正確:) – Goofyahead