我已经在我的应用程序中启动了来自位置API的活动识别意图服务。我希望在用户活动发生变化时启动服务。但是我似乎无法让它起作用。以下是我尝试过的内容:
在活动识别意图服务的Intent Service类中:(编辑:在if条件中添加了日志。)
在清单文件中:
我有一个按钮,用户可以点击手动启动服务,这个功能正常工作。但当我想要自动启动服务时,它就不起作用了。
应用程序没有崩溃,所以我没有任何日志错误可供发布。
如果您需要查看更多代码,请告诉我。
谢谢。
在活动识别意图服务的Intent Service类中:(编辑:在if条件中添加了日志。)
@Override
protected void onHandleIntent(Intent intent)
{
// Get the update
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// Get the most probable activity from the list of activities in the update
DetectedActivity mostProbableActivity = result.getMostProbableActivity();
// Get the type of activity
int currentActivity = mostProbableActivity.getType();
if(currentActivity == DetectedActivity.IN_VEHICLE)
{
Log.w("Rakshak", "in the if condition"); <-- this gets posted.
sendNotification(); // this just send a notification that the service has started.
Intent i = new Intent(this, MyService.class);
intent.setAction("start");
startService(i); <-- this dose'nt start the service.
}
}
在清单文件中:
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<application
....
<service android:name="driver.rakshak.drivetrackeradmin.MyService"/>
<service android:name="driver.rakshak.drivetrackeradmin.ActivityRecognitionIntentService"/>
</application>
我有一个按钮,用户可以点击手动启动服务,这个功能正常工作。但当我想要自动启动服务时,它就不起作用了。
应用程序没有崩溃,所以我没有任何日志错误可供发布。
如果您需要查看更多代码,请告诉我。
谢谢。