Commit e8c962e0 by cuiliang.shi

更新自动处理gps 和网络开启功能

1 parent 660f0b35
......@@ -11,6 +11,7 @@ public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("BirdService","receiver--"+System.currentTimeMillis());
Intent i = new Intent(context,BirdService.class);
context.startForegroundService(i);
DevicePreferences.getInstance(context).putBoolean("is_need_notification", false);
context.startService(i);
}
}
......@@ -51,7 +51,6 @@ public class BirdService extends Service {
private SubscriptionManager mSubscriptionManager;
private List<SubscriptionInfo> mSubList;
private String mStartTime;
private long mInterval;
public AMapLocationClient mLocationClient = null;
private String TAG = "BirdService";
private Context mContext;
......@@ -82,39 +81,37 @@ public class BirdService extends Service {
mContext = getApplicationContext();
String CHANNEL_ONE_ID = "com.primedu.cn";
String CHANNEL_ONE_NAME = "Channel One";
NotificationChannel notificationChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this).setChannelId(CHANNEL_ONE_ID)
.setContentIntent(pendingIntent)
.getNotification();
}
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(1, notification);
new Thread(new Runnable() {
@Override
public void run() {
stopForeground(true);
Log.d(TAG, "time:" + new Date().toString());
stopSelf();
// open gps & network.
// close gps & network.
Intent intent2 = new Intent("com.bird.open_gps_switch");
intent2.addFlags(0x01000000);
sendBroadcast(intent2);
if (DevicePreferences.getInstance(this).getBoolean("is_need_notification", true)){
String CHANNEL_ONE_ID = "com.primedu.cn";
String CHANNEL_ONE_NAME = "Channel One";
NotificationChannel notificationChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
}
}).start();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this).setChannelId(CHANNEL_ONE_ID)
.setContentIntent(pendingIntent)
.getNotification();
}
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(1, notification);
}
ret_cfgv = DevicePreferences.getInstance(getApplicationContext()).getString("bird_ret_cfgv", "0");
mPath = DevicePreferences.getInstance(mContext).getString("bird_location_upload_url", DEFAULTPATH);
......@@ -148,15 +145,14 @@ public class BirdService extends Service {
mLocationClient.stopLocation();
}
} else {
setAlarm();
// setAlarm();
Log.e(TAG, "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
if (mLocationClient != null) {
/* if (mLocationClient != null) {
mLocationClient.stopLocation();
}
}*/
}
} else {
Log.d(TAG, "location Error");
......@@ -173,7 +169,7 @@ public class BirdService extends Service {
Log.d(TAG, "ALARM - CONFIG - PARAM, time minutes: " + temp
+ ", cfgv: " + ret_cfgv + ", path: " + mPath);
mInterval = temp * 60 * 1000;
long mInterval = temp * 60 * 1000;
if (mInterval != 0) {
AlarmManager manger = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
......@@ -182,6 +178,10 @@ public class BirdService extends Service {
triggerAtTime = triggerAtTime + mInterval + 10;
manger.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pendingIntent2);
}
stopForeground(true);
Log.d(TAG, "time:" + new Date().toString());
stopSelf();
}
......@@ -267,7 +267,6 @@ public class BirdService extends Service {
} catch (Exception e) {
e.printStackTrace();
} finally {
setAlarm();
httpURLConnection.disconnect();
Log.d(TAG, "close connect");
if (os != null) {
......@@ -278,6 +277,13 @@ public class BirdService extends Service {
}
}
}
setAlarm();
// close gps & network.
Intent intent = new Intent("com.bird.close_gps_switch");
intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent);
}
}).start();
}
......
......@@ -8,7 +8,8 @@ import android.support.annotation.RequiresApi;
import android.util.Log;
public class LocationReceiver extends BroadcastReceiver{
private String TAG = "qqh";
private String TAG = "LocationReceiver";
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
......@@ -18,6 +19,7 @@ public class LocationReceiver extends BroadcastReceiver{
if (BirdService.status != 1){
Log.d("qqh","bird_sos_open");
Intent intentThree = new Intent(context, BirdService.class);
DevicePreferences.getInstance(context).putBoolean("is_need_notification", true);
context.startForegroundService(intentThree);
BirdService.status = 1;
}
......
......@@ -3,17 +3,11 @@ package com.agenew.location_server.application;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
......@@ -23,14 +17,10 @@ import com.agenew.location_server.application.permission.annotation.OnMPermissio
import com.agenew.location_server.application.permission.annotation.OnMPermissionGranted;
import com.agenew.location_server.application.permission.annotation.OnMPermissionNeverAskAgain;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity implements View.OnClickListener {
private String TAG="BirdService";
private static final String TAG="BirdService";
private SharedPreferences sp;
private LocationReceiver locationRecevier;
private Context mContext;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!