new thread in Titanium Hyperloop

199 views Asked by At

I want use Huawei Push Kit in my Appcelerator Titanium app with Hyperloop.

    var tokenString = '';
    var Activity = require('android.app.Activity');
    var ActivityToken = require('com.huawei.hms.aaid.HmsInstanceId');

try{    
        const activity = new Activity(Ti.Android.currentActivity);
        tokenString = ActivityToken.getInstance(activity).getToken(appID, "HCM");
        console.log('tokenString', tokenString); 
}
catch (e){
    console.log(e);
}

But I receive error: "operation in MAIN thread prohibited"

How do I run the code in a separate thread?

2

There are 2 answers

5
zhangxaochen On

You could try the Automatic Initialization, by calling the setAutoInitEnabled(boolean enable) method in HmsMessaging.

ActivityToken.getInstance(activity).setAutoInitEnabled(true);

The applied token is returned through the onNewToken() method after completing the configuration.

1
Zinna On

Wrap around the code inside background thread, since the error says clearly that this can not be run on main thread. Time consuming calls are usually not allowed in main thread, onCreate etc.

Thread { 
…
    }.run()