3

ActivityTransitionUpdate returns null, always. But if I change it to requestactivityupdate it works fine. But I need the transition API. I do not understand why this is not working, especially since a couple of days ago it was working right.

I have tried rewriting the code, looked everywhere on the web, copied code from other projects that apparently work, but the requestActivityTransitionUpdates does not want to work anymore. I have also uninstalled and reinstalled android studio, but nothing is working.

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

''''

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

''''

My Manifest ''''

<?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>

''''

My 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'
}

I expect to and was receiving transition updates when the app was open that were being stored in a csv file in the apps cache. Now my OnHandleIntent is not even being run from the task as it should be.

2 Answers2

0

Not sure about it, but maybe helpful:

transitionPendingIntent = PendingIntent.getService(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Task task = activityRecognitionClient.requestActivityUpdates(1000, transitionPendingIntent);
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I am a bit worried about that eating up battery. This is going to be running all day as a service and I am trying to ensure it's not detrimental to have running. Plus it does not give me the transitions. I just don't get why the transitions would just stop working. – Brian Pugliese Oct 25 '19 at 05:02
0

It happened to me the same thing before.It seems that last activity transition event kept in catch. Sometimes, you get an old event results from device's catch. So, to avoid all that problem, check the elapsed time of when the transition occurred, and ignore the callback if the transition happened over 30 seconds ago.

if (intent != null) {
        if (ActivityTransitionResult.hasResult(intent)) {
            ActivityTransitionResult result = ActivityTransitionResult.extractResult(intent);
            for (ActivityTransitionEvent event : result.getTransitionEvents()) {

                //if event occurred less than 30 seconds, get the event. Sometimes app gets old events from device's catch
                //that is why you have to make sure that the event is not that old.
                if(((SystemClock.elapsedRealtime()-(event.getElapsedRealTimeNanos()/1000000))/1000) <= 30) {
                   //Do your staff here......
                   ............................
                   ............................
                   ...............................
                }

            }
        }
    }
    else {
        Log.d(TAG, "onHandleIntent: Banana");
    }
Yosidroid
  • 2,053
  • 16
  • 15