我如何将IntentService转换为即使在应用程序被终止后仍应运行的后台服务

时间:2018-09-12 10:59:18

标签: android android-pendingintent intentservice activity-recognition

我正在DetectedActivity上工作,目前在此方面取得了成功的结果。现在我想获取我的应用程序被杀死时用户检测到的活动(日志或吐司)。我已经阅读了ActivityRecognization"IntentService"的api的Google文档。因此,即使我的应用程序被杀死,我也不知道如何在烤面包中获取detectedActivity的“信心”值。有人可以帮我吗?我的代码如下。

ActivityIntentService

"PendingIntent"

我在活动班级中使用此public class ActivityIntentService extends IntentService { protected static final String TAG = "Activity"; static int confidence; static int activityType; static String test; //Call the super IntentService constructor with the name for the worker thread// public ActivityIntentService() { super(TAG); } @Override public void onCreate() { super.onCreate(); } //Define an onHandleIntent() method, which will be called whenever an activity detection update is available// @Override protected void onHandleIntent(Intent intent) { //Check whether the Intent contains activity recognition data// if (ActivityRecognitionResult.hasResult(intent)) { //If data is available, then extract the ActivityRecognitionResult from the Intent// ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); //Get an array of DetectedActivity objects// ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities(); PreferenceManager.getDefaultSharedPreferences(this) .edit() .putString(MainActivity.DETECTED_ACTIVITY, detectedActivitiesToJson(detectedActivities)) .apply(); DetectedActivity mostProbableActivity; mostProbableActivity = result.getMostProbableActivity(); confidence = mostProbableActivity.getConfidence(); activityType = mostProbableActivity.getType(); Toast.makeText(this,"DETECTION IS RUNNING",Toast.LENGTH_LONG).show(); if (mostProbableActivity.equals("STILL") &&confidence > 80) { Toast.makeText(this,"STILL",Toast.LENGTH_LONG).show(); } } } //Convert the code for the detected activity type, into the corresponding string// @SuppressLint("StringFormatInvalid") static String getActivityString(Context context, int detectedActivityType) { Resources resources = context.getResources(); switch(detectedActivityType) { case DetectedActivity.ON_BICYCLE: return resources.getString(R.string.bicycle); case DetectedActivity.ON_FOOT: return resources.getString(R.string.foot); case DetectedActivity.RUNNING: return resources.getString(R.string.running); case DetectedActivity.STILL: return resources.getString(R.string.still); case DetectedActivity.TILTING: return resources.getString(R.string.tilting); case DetectedActivity.WALKING: return resources.getString(R.string.walking); case DetectedActivity.IN_VEHICLE: return resources.getString(R.string.vehicle); default: return resources.getString(R.string.unknown_activity, detectedActivityType); } } static final int[] POSSIBLE_ACTIVITIES = { DetectedActivity.STILL, DetectedActivity.ON_FOOT, DetectedActivity.WALKING, DetectedActivity.RUNNING, DetectedActivity.IN_VEHICLE, DetectedActivity.ON_BICYCLE, DetectedActivity.TILTING, DetectedActivity.UNKNOWN }; static String detectedActivitiesToJson(ArrayList<DetectedActivity> detectedActivitiesList) { Type type = new TypeToken<ArrayList<DetectedActivity>>() {}.getType(); return new Gson().toJson(detectedActivitiesList, type); } static ArrayList<DetectedActivity> detectedActivitiesFromJson(String jsonArray) { Type listType = new TypeToken<ArrayList<DetectedActivity>>(){}.getType(); ArrayList<DetectedActivity> detectedActivities = new Gson().fromJson(jsonArray, listType); if (detectedActivities == null) { detectedActivities = new ArrayList<>(); } return detectedActivities; } } ,如下所示:

service

请有人帮我。预先谢谢你

0 个答案:

没有答案