ActivityTransitionUpdate всегда возвращает ноль, всегда. Но если я изменю его на requesttactivity update, он будет работать нормально. Но мне нужен API перехода. Я не понимаю, почему это не работает, тем более, что пару дней назад это работало правильно.
Я пытался переписать код, посмотрел везде в Интернете, скопировал код из других проектов, которые, очевидно, работают, но requestActivityTransitionUpdates больше не хочет работать. Я также удалил и переустановил Android Studio, но ничего не работает.
Основная активность '' ''
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());
Log.i(TAG, "ON");
mContext = this;
activityRecognitionClient = ActivityRecognition.getClient(mContext);
Intent intent = new Intent(mContext, 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;
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();
}
});
//startService(intent);
}
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();
}
});
}
'' ''
IntentService
'' ''
public TransitionIntentService(String name) {
super(name);
}
private int activityBang;
private int transitionBang;
@Override
public void onHandleIntent(Intent intent) {
Log.i(TAG, "onHandleIntent: GO");
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();
}
}
}
}
else {
Log.d(TAG, "onHandleIntent: Banana");
}
}
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();
}
'' ''
Мой манифест '' ''
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.a200000"
tools:ignore="GoogleAppIndexingWarning">
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".TransitionIntentService"
android:exported="false" />
</application>
</manifest>
'' ''
Мой Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.a200000"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.opencsv:opencsv:4.0'
}
Я ожидаю и получал обновления переходов, когда приложение было открыто и сохранялось в файле csv в кэше приложений. Теперь мой OnHandleIntent даже не запускается из задачи, как должно быть.