Java tutorial
/* * Copyright 2017 Alex_ZHOU * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alex.vmandroid.services; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import com.alex.businesses.UploadRecordDBBiz; import com.alex.utils.AppLog; import com.alex.utils.TimeMeter; import com.alex.vmandroid.R; import com.alex.vmandroid.databases.UserInfo; import com.alex.vmandroid.display.voice.RecordDBTool; import com.alex.vmandroid.display.voice.db.RecordDBActivity; import com.alex.vmandroid.entities.RecordDB; import com.alex.vmandroid.receivers.RecordDBReceiver; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; /** * ?????? */ public class RecordDBService extends Service implements AMapLocationListener { private static final String TAG = RecordDBService.class.getName(); public static final String RecordDBServiceName = "com.alex.vmandroid.services.RecordDBService"; public static final String RecordDBServiceAction = "com.alex.vmandroid.services.RecordDBServiceAction"; public static final String RECORD_DB_SERVICE_DB = "RECORD_DB_SERVICE_DB"; public static final String RECORD_DB_SERVICE_TIME = "RECORD_DB_SERVICE_TIME"; public static final String RECORD_DB_SERVICE_MAX_DB = "RECORD_DB_SERVICE_MAX_DB"; public static final String RECORD_DB_SERVICE_AVERAGE_DB = "RECORD_DB_SERVICE_AVERAGE_DB"; public static final String RECORD_DB_SERVICE_UPLOAD_SUCCEED = "RECORD_DB_SERVICE_UPLOAD_SUCCEED"; /** * */ private RecordDBReceiver mReceiver; private MessageReceiver mReceiver2; /** * ? */ private long mTotalDB = 0L; /** * ? */ private long mRecordDBTimes = 0; /** * ?? */ private int mMaxDB = 0; /** * AMapLocationClient */ private AMapLocationClient mLocationClient = null; /** * */ private double mLatitude; /** * ? */ private double mLongitude; /** * */ private List<RecordDB> mRecordDBList = new ArrayList<>(); private RecordDBTool mRecordDBTool = new RecordDBTool(new RecordDBTool.Listener() { @Override public void onDB(int db) { AppLog.info(TAG, "The db is " + db + "."); Intent intent = new Intent(RecordDBService.RecordDBServiceAction); intent.putExtra(RecordDBService.RECORD_DB_SERVICE_DB, db); sendBroadcast(intent); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); String time = df.format(new Date()); mRecordDBTimes++; mTotalDB += db; mMaxDB = mMaxDB > db ? mMaxDB : db; int averageDB = (int) (mTotalDB / mRecordDBTimes); intent = new Intent(RecordDBService.RecordDBServiceAction); intent.putExtra(RecordDBService.RECORD_DB_SERVICE_MAX_DB, mMaxDB); intent.putExtra(RecordDBService.RECORD_DB_SERVICE_AVERAGE_DB, averageDB); sendBroadcast(intent); RecordDB recordDB = new RecordDB(); recordDB.setDb(db); recordDB.setLatitude(mLatitude); recordDB.setLongitude(mLongitude); recordDB.setTime(time); recordDB.setTimekeeper(mTimeMeter.getTime()); mRecordDBList.add(recordDB); } }); private TimeMeter mTimeMeter = new TimeMeter(new TimeMeter.CallBack() { @Override public void getTime(String time) { AppLog.info(TAG, "The time is " + time + "."); Intent intent = new Intent(RecordDBService.RecordDBServiceAction); intent.putExtra(RecordDBService.RECORD_DB_SERVICE_TIME, time); sendBroadcast(intent); } }); @Override public void onCreate() { super.onCreate(); //region ???? NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); Intent intent = new Intent(this, RecordDBActivity.class); //PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); mBuilder.setSmallIcon(R.drawable.vm_android_app_icon).setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.record)).setContentIntent(pendingIntent); Notification mNotification = mBuilder.build(); // ? //mNotification.icon = R.drawable.icon_upload_location; //?? mNotification.flags = Notification.FLAG_ONGOING_EVENT;//FLAG_ONGOING_EVENT ?? FLAG_AUTO_CANCEL ?? //???Light mNotification.defaults = Notification.DEFAULT_VIBRATE; //?? mNotification.tickerText = "???"; //? mNotification.when = System.currentTimeMillis(); // ? //mNotification.icon = R.drawable.icon_upload_location; //?? mNotification.flags = Notification.FLAG_ONGOING_EVENT;//FLAG_ONGOING_EVENT ?? FLAG_AUTO_CANCEL ?? //???Light mNotification.defaults = Notification.DEFAULT_VIBRATE; //?? mNotification.tickerText = "???"; //? mNotification.when = System.currentTimeMillis(); //id?? int notifyId = 1001; startForeground(notifyId, mNotification); //endregion //region ? mReceiver = new RecordDBReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(RecordDBService.RecordDBServiceAction); registerReceiver(mReceiver, intentFilter); mReceiver2 = new MessageReceiver(); intentFilter = new IntentFilter(); intentFilter.addAction(RecordDBReceiver.ACTION); registerReceiver(mReceiver2, intentFilter); //endregion //region ?? mLocationClient = new AMapLocationClient(this); //??? AMapLocationClientOption locationOption = new AMapLocationClientOption(); //?? mLocationClient.setLocationListener(this); //???Battery_Saving?Device_Sensors? locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //?,??,2000ms locationOption.setInterval(2000); //?? mLocationClient.setLocationOption(locationOption); //endregion } @Nullable @Override public IBinder onBind(Intent intent) { AppLog.debug("RecordDBService onBind() is called."); return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { AppLog.debug("RecordDBService onStartCommand() is called."); //? mRecordDBTool.start(); //? mTimeMeter.start(); //?? mLocationClient.startLocation(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { AppLog.debug("RecordDBService onDestroy() is called."); unregisterReceiver(mReceiver); unregisterReceiver(mReceiver2); super.onDestroy(); } private void uploadRecordInfo(List<RecordDB> recordDBList) { mRecordDBTool.close(); mTimeMeter.close(); mLocationClient.stopLocation(); mLocationClient.onDestroy(); UploadRecordDBBiz.commit(recordDBList, UserInfo.getUsrId(getApplicationContext()), UserInfo.getInt(this, "RecordTimes"), new UploadRecordDBBiz.Listener() { @Override public void succeed() { Intent intent = new Intent(RecordDBService.RecordDBServiceAction); intent.putExtra(RecordDBService.RECORD_DB_SERVICE_UPLOAD_SUCCEED, true); sendBroadcast(intent); Intent intent2 = new Intent(getApplicationContext(), RecordDBService.class); stopService(intent2); } @Override public void failure() { // TODO Intent intent = new Intent(RecordDBService.RecordDBServiceAction); intent.putExtra(RecordDBService.RECORD_DB_SERVICE_UPLOAD_SUCCEED, false); sendBroadcast(intent); // ???? Intent intent2 = new Intent(getApplicationContext(), RecordDBService.class); stopService(intent2); } }); } @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //???? mLatitude = amapLocation.getLatitude();//? mLongitude = amapLocation.getLongitude();//?? AppLog.info(TAG, "Latitude:" + mLatitude + " ,Longitude" + mLongitude); } else { //?ErrCode?errInfo??? AppLog.error("Amap Location Error" + ", ErrCode:" + amapLocation.getErrorCode() + ", ErrInfo:" + amapLocation.getErrorInfo()); } } } private class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(RecordDBReceiver.ACTION)) { if (intent.getExtras().containsKey(RecordDBReceiver.RECORD_DB_UPLOAD_DATA)) { boolean b = intent.getBooleanExtra(RecordDBReceiver.RECORD_DB_UPLOAD_DATA, false); if (b) { uploadRecordInfo(mRecordDBList); } } } } } }