|
|
@@ -0,0 +1,281 @@
|
|
|
+package com.usai.redant.rautils.Receiver;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+public abstract class AlarmReceiver extends BootCompleteBroadcastReceiver {
|
|
|
+ protected int alarm_timeInterval = 30 * 1000;
|
|
|
+
|
|
|
+// protected static final int SO_TIMEOUT = 15 * 1000;
|
|
|
+
|
|
|
+ protected abstract void InitAlarm(Context context, Intent intent);
|
|
|
+ protected abstract void AlarmProc(Context context, Intent intent);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+
|
|
|
+ super.onReceive(context,intent);
|
|
|
+
|
|
|
+ String action = intent
|
|
|
+ .getAction();
|
|
|
+
|
|
|
+ Log.d("AlarmReceiver", "onReceive: " + action);
|
|
|
+
|
|
|
+ if (RABroadcast.ACTION_REDANT_INIT_ALARM.equals(intent.getAction())) {
|
|
|
+ InitAlarm(context,intent);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ if (RABroadcast.ACTION_REDANT_ALARM.equals(intent.getAction())) {
|
|
|
+
|
|
|
+
|
|
|
+ AlarmProc(context,intent);
|
|
|
+
|
|
|
+ }
|
|
|
+// else
|
|
|
+// if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) {
|
|
|
+//
|
|
|
+// InitAlarm(context,intent);
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+
|
|
|
+?????
|
|
|
+
|
|
|
+ Intent iAlarm = new Intent(context, AlarmReceiver.class);
|
|
|
+// iAlarm.putExtra("caller", caller);
|
|
|
+ iAlarm.setAction("ACTION_PUSHNOTIFICATION_CHECK");
|
|
|
+ PendingIntent sender = PendingIntent.getBroadcast(context, 0,
|
|
|
+ iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+
|
|
|
+
|
|
|
+ AlarmManager am = (AlarmManager) context.getSystemService(
|
|
|
+ Context.ALARM_SERVICE);
|
|
|
+// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
+// am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + pushcheck_timeInterval, sender);
|
|
|
+// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
+// am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + pushcheck_timeInterval, sender);
|
|
|
+// }
|
|
|
+
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
+ am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + alarm_timeInterval, sender);
|
|
|
+ } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
+ am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + alarm_timeInterval, sender);
|
|
|
+ }
|
|
|
+
|
|
|
+*/
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void OnBootComplete(Context context, Intent intent) {
|
|
|
+
|
|
|
+ Log.d("AlarmReceiver", "OnBootComplete: SEND" + RABroadcast.ACTION_REDANT_INIT_ALARM);
|
|
|
+ Intent bintent = new Intent(RABroadcast.ACTION_REDANT_INIT_ALARM);
|
|
|
+// bintent.putExtra("msg", msg.toString());
|
|
|
+
|
|
|
+ bintent.setClass(context, this.getClass());
|
|
|
+ context.sendBroadcast(bintent);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void OnLockedBootComplete(Context context, Intent intent) {
|
|
|
+
|
|
|
+ Log.d("AlarmReceiver", "OnLockedBootComplete: SEND" + RABroadcast.ACTION_REDANT_INIT_ALARM);
|
|
|
+
|
|
|
+ Log.d("AlarmReceiver", "OnLockedBootComplete: class" + this.getClass());
|
|
|
+ Intent bintent = new Intent(RABroadcast.ACTION_REDANT_INIT_ALARM);
|
|
|
+// bintent.putExtra("msg", msg.toString());
|
|
|
+
|
|
|
+
|
|
|
+ bintent.setClass(context, this.getClass());
|
|
|
+ context.sendBroadcast(bintent);
|
|
|
+ }
|
|
|
+
|
|
|
+/*
|
|
|
+
|
|
|
+ private String getDeviceId(Context context) {
|
|
|
+ TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ // TODO: Consider calling
|
|
|
+ // ActivityCompat#requestPermissions
|
|
|
+ // here to request the missing permissions, and then overriding
|
|
|
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
+ // int[] grantResults)
|
|
|
+ // to handle the case where the user grants the permission. See the documentation
|
|
|
+ // for ActivityCompat#requestPermissions for more details.
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String deviceId = telephonyManager.getDeviceId().toString();
|
|
|
+ return deviceId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void check_push(Context context)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ String dev_id = getDeviceId(context);
|
|
|
+ Log.d("AlarmReceiver", "check_push: "+dev_id);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ logout();
|
|
|
+
|
|
|
+// runOnUiThread(new Runnable() {
|
|
|
+// @Override
|
|
|
+// public void run() {
|
|
|
+// dismissProgressDialog();
|
|
|
+// ApexDriverApplication.sharedApplication().logout();
|
|
|
+// showLogin();
|
|
|
+// }
|
|
|
+// });
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+// return dev_id;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private String getJson(String url, Bundle parms)
|
|
|
+ {
|
|
|
+ String TAG = "net_dbg@GetJson";
|
|
|
+ Log.d(TAG, "entry");
|
|
|
+ String ret=null;
|
|
|
+
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+
|
|
|
+ URL _url;
|
|
|
+ _url = new URL(url);
|
|
|
+ connection = (HttpURLConnection) _url.openConnection();
|
|
|
+ connection.setReadTimeout(SO_TIMEOUT);
|
|
|
+ connection.setConnectTimeout(SO_TIMEOUT);
|
|
|
+ // 设置请求方式
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ // 设置编码格式
|
|
|
+ connection.setRequestProperty("Charset", "UTF-8");
|
|
|
+ // 传递自定义参数
|
|
|
+// connection.setRequestProperty("MyProperty", "this is me!");
|
|
|
+
|
|
|
+ Set<String> keys = parms.keySet();
|
|
|
+ Log.d(TAG, "================parms============");
|
|
|
+ for (String key : keys)
|
|
|
+ {
|
|
|
+ if (key.contains("_file"))
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Log.d(TAG, "key=" + key + " val=" + parms.get(key).toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ Log.d(TAG, "================parms============");
|
|
|
+
|
|
|
+ Log.d(TAG,"URL: "+url);
|
|
|
+ // 设置容许输出
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+
|
|
|
+
|
|
|
+ OutputStream os = connection.getOutputStream();
|
|
|
+ BufferedWriter writer = new BufferedWriter(
|
|
|
+ new OutputStreamWriter(os, "UTF-8"));
|
|
|
+ writer.write(createPostParameters(parms));
|
|
|
+
|
|
|
+ writer.flush();
|
|
|
+ writer.close();
|
|
|
+ os.close();
|
|
|
+
|
|
|
+ // 获取返回数据
|
|
|
+ if(connection.getResponseCode() == 200){
|
|
|
+ InputStream is = connection.getInputStream();
|
|
|
+
|
|
|
+ BufferedReader br = new BufferedReader(
|
|
|
+ new InputStreamReader(is, "utf-8"), 8);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ String line = null;
|
|
|
+ while ((line = br.readLine()) != null)
|
|
|
+ {
|
|
|
+ sb.append(line + "\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d(TAG, "Response: content begin");
|
|
|
+ Log.d(TAG, sb.toString());
|
|
|
+ Log.d(TAG, "Response: content end");
|
|
|
+
|
|
|
+ if (sb.length() <= 0)
|
|
|
+ {
|
|
|
+
|
|
|
+ ret= null;
|
|
|
+ }
|
|
|
+ ret= sb.toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ Log.e(TAG, e.toString());
|
|
|
+
|
|
|
+ } catch (ProtocolException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ Log.e(TAG, e.toString());
|
|
|
+
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ Log.e(TAG, e.toString());
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ Log.e(TAG, e.toString());
|
|
|
+
|
|
|
+ } finally {
|
|
|
+
|
|
|
+ if(connection!=null){
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ private String createPostParameters(Bundle parms) throws UnsupportedEncodingException {
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ boolean first = true;
|
|
|
+ for(String key : parms.keySet()){
|
|
|
+ if (first)
|
|
|
+ first = false;
|
|
|
+ else
|
|
|
+ result.append("&");
|
|
|
+
|
|
|
+ result.append(URLEncoder.encode(key, "UTF-8"));
|
|
|
+ result.append("=");
|
|
|
+ result.append(URLEncoder.encode(parms.get(key).toString(), "UTF-8"));
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+ private static final String URL_HOST = "http://192.168.0.124:8080";
|
|
|
+ public static final String URL_LOGOUT = URL_HOST + "/j/mobile/loginOut.mo/";
|
|
|
+ public void logout() {
|
|
|
+
|
|
|
+
|
|
|
+ Bundle params = new Bundle();
|
|
|
+ params.putString("platform","android");
|
|
|
+
|
|
|
+ getJson(URL_LOGOUT,params);
|
|
|
+ }
|
|
|
+
|
|
|
+ */
|
|
|
+}
|