如何让任务调用 pendingIntent
How to get Task to call pendingIntent
我一直在尝试将 pendingIntent 作为任务的一部分传递给 Activity 识别客户端,但是 pendingIntent 似乎没有被调用。
我正在使用 Google 的 Activity 识别客户端 (https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionClient). I have already looked at the example code (found here: https://github.com/googlesamples/android-play-location/blob/master/ActivityRecognition/app/src/main/java/com/google/android/gms/location/sample/activityrecognition/MainActivity.java),但似乎看不到我的问题。
我的代码如下所示(我提取了 class 的部分内容以使其更易于阅读):
public class SignIn extends AppCompatActivity {
private ActivityRecognitionClient mActivityRecognitionClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
mActivityRecognitionClient = new ActivityRecognitionClient(this);
Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(1000L, getActivityDetectionPendingIntent());
task.addOnSuccessListener(result -> Log.i("test", "test"));
task.addOnFailureListener(e -> Log.e("test", e.toString()));
}
private PendingIntent getActivityDetectionPendingIntent() {
Intent intent = new Intent(this, DetectedActivitiesIntentService.class);
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
package com.example.kangaroute;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import java.util.ArrayList;
class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = "DetectedActivity";
public DetectedActivitiesIntentService(){
super(TAG);
}
@Override
public void onCreate(){
Log.i("test", "works");
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// Get the list of the probable activities associated with the current state of the
// device. Each activity is associated with a confidence level, which is an int between
// 0 and 100.
ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();
for (DetectedActivity activity : detectedActivities){
Log.i(TAG, activity.toString());
}
}
}
当我 运行 代码时,我希望看到 "works" 被打印到 Logcat,因为我从 DetectedActivitiesIntentService
中的 onCreate()
方法记录它=] class。然而,我什么也没看到。
但是,"test" 是从与任务关联的 OnSuccessListener 记录的。当我记录结果时,没有任何结果...
大声笑猜猜谁忘了放 "public class" 而只是放 "class"。解决了!
我一直在尝试将 pendingIntent 作为任务的一部分传递给 Activity 识别客户端,但是 pendingIntent 似乎没有被调用。
我正在使用 Google 的 Activity 识别客户端 (https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionClient). I have already looked at the example code (found here: https://github.com/googlesamples/android-play-location/blob/master/ActivityRecognition/app/src/main/java/com/google/android/gms/location/sample/activityrecognition/MainActivity.java),但似乎看不到我的问题。
我的代码如下所示(我提取了 class 的部分内容以使其更易于阅读):
public class SignIn extends AppCompatActivity {
private ActivityRecognitionClient mActivityRecognitionClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
mActivityRecognitionClient = new ActivityRecognitionClient(this);
Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(1000L, getActivityDetectionPendingIntent());
task.addOnSuccessListener(result -> Log.i("test", "test"));
task.addOnFailureListener(e -> Log.e("test", e.toString()));
}
private PendingIntent getActivityDetectionPendingIntent() {
Intent intent = new Intent(this, DetectedActivitiesIntentService.class);
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
package com.example.kangaroute;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import java.util.ArrayList;
class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = "DetectedActivity";
public DetectedActivitiesIntentService(){
super(TAG);
}
@Override
public void onCreate(){
Log.i("test", "works");
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// Get the list of the probable activities associated with the current state of the
// device. Each activity is associated with a confidence level, which is an int between
// 0 and 100.
ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();
for (DetectedActivity activity : detectedActivities){
Log.i(TAG, activity.toString());
}
}
}
当我 运行 代码时,我希望看到 "works" 被打印到 Logcat,因为我从 DetectedActivitiesIntentService
中的 onCreate()
方法记录它=] class。然而,我什么也没看到。
但是,"test" 是从与任务关联的 OnSuccessListener 记录的。当我记录结果时,没有任何结果...
大声笑猜猜谁忘了放 "public class" 而只是放 "class"。解决了!