gpt4 book ai didi

android - Activity Transition API 调用 Intent

转载 作者:行者123 更新时间:2023-12-05 07:17:58 25 4
gpt4 key购买 nike

ActivityTransitionUpdate 始终返回 null。但是,如果我将其更改为 requestactivityupdate,它就可以正常工作。但我需要转换 API。我不明白为什么这不起作用,特别是几天前它工作正常。

我尝试过重写代码,在网络上到处查看,从其他项目中复制显然有效的代码,但 requestActivityTransitionUpdates 不想再工作了。我还卸载并重新安装了 android studio,但没有任何效果。

主要 Activity ''''

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();
}
});
}

''''

Intent 服务

''''

 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();
}

''''

我的 list ''''

<?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 甚至没有从任务中运行。

最佳答案

不确定,但可能有帮助:

transitionPendingIntent = PendingIntent.getService(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Task task = activityRecognitionClient.requestActivityUpdates(1000, transitionPendingIntent);

关于android - Activity Transition API 调用 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58551986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com