Commit e8c962e0 by cuiliang.shi

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

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