我正在開發一款應用程序,該應用程序將在適當的時間發送通知。如果我在主要活動中調用AlarmManager.init(),它會在三星Galaxy Tab 3和LeEco Le 2上5秒後給我Toast,因爲它應該如此。但是,如果我打電話給AlarmManager.init()並殺死我的應用程序(將它刷掉) - 我仍然在三星Galaxy Tab 3上得到Toast,但我沒有在LeEco Le 2上得到它。我的問題是什麼?在三星Galaxy Tab 3上調用鬧鐘,但未呼叫LeEco Le2
AlarmManager:
public class AlarmManager {
static android.app.AlarmManager alarmManager;
static Context context;
public static void init (Context c) {
context = c;
alarmManager = (android.app.AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent intent = new Intent("com.fome.planner");
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
5 * 1000, alarmIntent);
}
}
接收機:
public class MyReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.e("type", intent.getAction());
StringBuilder msgStr = new StringBuilder("current time: ");
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));
Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show();
}
}
清單:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity android:name=".DayActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_places_key" />
<activity android:name=".TaskCreationActivity" />
<activity android:name=".CalendarActivity" />
<activity android:name=".ListActivity" />
<receiver
android:name=".MyReceiver"
android:process=":remote"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.fome.planner"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
如果我不得不猜測,LeEco做了一些愚蠢的事情,並將應用程序輕掃行爲與設置中應用頁面上的「強制停止」按鈕背後的邏輯關聯起來。您可以稍微延長一段時間(例如,一分鐘)並再次嘗試。在等待鬧鐘響起時,使用'adb shell dumpsys alarm'來查看鬧鐘是否顯示。如果警報丟失,則它們取消警報(例如,作爲強制停止處理的一部分)。如果警報在1分鐘後出現並且熄滅,則可能是節電的最小警報時間。如果警報存在並且不會熄滅...? – CommonsWare
我已將時間設置爲1分鐘,啓動了我的應用並運行「adb shell dumpsys alarm」。它給了我一個警報列表,但我沒有在其中找到我的應用程序。而我甚至沒有殺死我的應用程序。而且鬧鐘已經叫做(Toast出現)。 –
也許那個設備比我想象的更糟。 – CommonsWare