Onhandleintent不再被调用
我尝试重新制作该程序,但仍然无法正常工作。我对此很陌生,但是很高兴能在周末工作。我只知道为什么它将停止工作...
我的主要方法
公共类MainActivity扩展了AppCompatActivity {
private List<ActivityTransition> transitions;
private ActivityRecognitionClient activityRecognitionClient;
private PendingIntent transitionPendingIntent;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(getExternalCacheDir());
mContext = this;
activityRecognitionClient = ActivityRecognition.getClient(mContext);
Intent intent = new Intent(this, TransitionIntentService.class);
transitionPendingIntent = PendingIntent.getService(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public void registerHandler(View view) {
transitions = new ArrayList<>();
transitions.add(new ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(new ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
transitions.add(new ActivityTransition.Builder()
.setActivityType(DetectedActivity.STILL)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(new ActivityTransition.Builder()
.setActivityType(DetectedActivity.STILL)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
transitions.add(new ActivityTransition.Builder()
.setActivityType(DetectedActivity.RUNNING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(new ActivityTransition.Builder()
.setActivityType(DetectedActivity.RUNNING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
transitions.add(new ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(new ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
ActivityTransitionRequest activityTransitionRequest
= new ActivityTransitionRequest(transitions);
Task<Void> task = activityRecognitionClient.requestActivityTransitionUpdates(activityTransitionRequest, transitionPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(mContext, "Transition update set up", Toast.LENGTH_LONG).show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(mContext, "Transition update Failed to set up", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
});
}
public void deregisterHandler(View view) {
Task<Void> task = activityRecognitionClient.removeActivityTransitionUpdates(transitionPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
transitionPendingIntent.cancel();
Toast.makeText(mContext, "Remove Activity Transition Successfully", Toast.LENGTH_LONG).show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(mContext, "Remove Activity Transition Failed", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
});
}
}
我的意图
公共类TransitionIntentService扩展了IntentService {
private int activityBang;
private int transitionBang;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public TransitionIntentService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
if (ActivityTransitionResult.hasResult(intent)) {
ActivityTransitionResult result = ActivityTransitionResult.extractResult(intent);
for (ActivityTransitionEvent event : result.getTransitionEvents()) {
Toast.makeText(this, event.getTransitionType() + "-" + event.getActivityType(), Toast.LENGTH_LONG).show();
//7 for walking and 8 for running
Log.i(TAG, "Activity Type " + event.getActivityType());
activityBang = event.getActivityType();
// 0 for enter, 1 for exit
Log.i(TAG, "Transition Type " + event.getTransitionType());
transitionBang = event.getTransitionType();
try {
ActivityWriter();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private void ActivityWriter() throws IOException {
String baseDir = getExternalCacheDir().toString();
String fileName = "SentinelData.csv";
String filePath = baseDir + File.separator + fileName;
File f = new File(filePath);
CSVWriter writer;
SimpleDateFormat date1 = new SimpleDateFormat("yyyy.MM.dd G");
SimpleDateFormat time1 = new SimpleDateFormat("HH:mm:ss z");
String date = date1.format(new Date());
String time = time1.format(new Date());
if(f.exists()&&!f.isDirectory())
{
FileWriter mFileWriter = new FileWriter(filePath, true);
writer = new CSVWriter(mFileWriter);
}
else
{
writer = new CSVWriter(new FileWriter(filePath));
}
String[] data = {date, time, String.valueOf(activityBang), String.valueOf(transitionBang)};
writer.writeNext(data);
writer.close();
}
}