我有實現報警經理到我的應用程序,但困惑,因爲我的告警管理器將無法啓動時,我的應用程序關閉/去世 我在那裏搜索在谷歌的許多建議,但沒有一個建議工作。這 我secenario報警管理器將無法啓動應用程序時關閉
- 開放的應用程序 - >自動啓動服務/鬧鐘經理
- 當應用程序中打開每10分鐘檢查應用服務器下載數據並插入到數據庫
- 時,每10分鐘一班應用程序關閉應用程序檢查到服務器下載數據並插入到數據庫中
問題是當應用程序關閉時服務也停止。 此我的示例代碼
MainActivity.java
AlarmReceiver alarm = new AlarmReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarm.setAlarm(this);
}
AlarmReceiver.Java
public class AlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, SchedulingService.class);
startWakefulService(context, service);
}
public void setAlarm(Context context) {
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
10000,
10000, alarmIntent);
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void cancelAlarm(Context context) {
// If the alarm has been set, cancel it.
if (alarmMgr!= null) {
alarmMgr.cancel(alarmIntent);
}
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
} }
BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
AlarmReceiver alarm = new AlarmReceiver();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.setAlarm(context);
}
}}
ScheduleService.java
public class SchedulingService extends IntentService {
public SchedulingService() {
super("SchedulingService");
}
public static final String TAG = "Scheduling Demo";
public static final int NOTIFICATION_ID = 1;
public static final String SEARCH_STRING = "Active";
public static final String URL = "http://localhost/TMALive";
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
@Override
protected void onHandleIntent(Intent intent) {
String urlString = URL;
String result ="";
try {
result = loadFromNetwork(urlString);
} catch (IOException e) {
Log.i(TAG, getString(R.string.connection_error));
}
if (result.indexOf(SEARCH_STRING) != -1) {
sendNotification(getString(R.string.live_found));
Log.i(TAG, "Your Post Live!!");
} else {
sendNotification(getString(R.string.no_live));
Log.i(TAG, "Your Post Off. :-(");
}
AlarmReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.pos_alert))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
private String loadFromNetwork(String urlString) throws IOException {
InputStream stream = null;
String str ="";
try {
stream = downloadUrl(urlString);
str = readIt(stream);
} finally {
if (stream != null) {
stream.close();
}
}
return str;
}
private InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Start the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
private String readIt(InputStream stream) throws IOException {
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
for(String line = reader.readLine(); line != null; line = reader.readLine())
builder.append(line);
reader.close();
return builder.toString();
}}
這就是代碼工作正常,當應用程序打開,但是當應用程序關閉顯示,並重新啓動後也沒有通知。
你能給我建議女巫更好的使用這種方法或使用服務的方法呢?
千恩萬謝
那麼,我做了什麼文檔在這裏說:https://developer.android.com/training/scheduling/alarms.html。到目前爲止,它工作得很好。即使手機重新啓動或應用程序未運行。 –
一些魅族手機具有自能源經理電話時關閉屏幕 – Beyka
這就是意味着更好的使用服務,可以破壞你的報警?但是如果使用服務如何每10分鐘創建一次服務數據? – Kenjin