我已经为Titanium创建了一个模块,用于将数据从我的应用程序发送到我的Android Wear应用程序,该应用程序运行良好。但是当我卸载Android应用程序崩溃时。我已经检查过Logcat,但我不知道问题到底是什么。如果我在调用module.sendData()之后立即卸载应用程序(参见示例)
,Android似乎只会崩溃示例:
index.js(app)
function onOpen() {
var module = require('teunozz.test.module')
module.addEventListener('connected', function f() {
console.log('send data');
module.sendData();
});
module.start();
}
$.index.open();
index.xml(app)
<Alloy>
<Window id="index" onOpen="onOpen">
</Window>
</Alloy>
AndroidWearClient(模块)
package teunozz.test.module;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.TiActivityResultHandler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
public class AndroidWearClient extends Object implements
TiActivityResultHandler,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
{
private final static String TAG = "AndroidWearClient";
private static final int REQUEST_OAUTH = 1;
private GoogleApiClient client = null;
private TitestModule module = null;
private boolean authInProgress = false;
private void buildClient() {
client = new GoogleApiClient.Builder(
TiApplication.getAppCurrentActivity())
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
public void start(TitestModule moduleArg) {
module = moduleArg;
buildClient();
client.connect();
}
public void stop() {
}
public void onError(Activity activity, int requestCode, Exception e) {
}
public void onResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_OAUTH) {
authInProgress = false;
if (resultCode == Activity.RESULT_OK) {
if (!client.isConnecting() && !client.isConnected()) {
client.connect();
}
}
}
}
public void onConnectionFailed(ConnectionResult result) {
}
public void onConnected(Bundle connectionHint) {
// connected to Google Wearable API
module.fireEvent("connected", null);
}
public void onConnectionSuspended(int cause) {
}
public void sendData() {
// Construct a DataRequest and send over the data layer
PutDataMapRequest putDMR = PutDataMapRequest.create("/test");
putDMR.getDataMap().putString("key", "test");
PutDataRequest request = putDMR.asPutDataRequest();
DataApi.DataItemResult result =
// this is the problematic bit if you comment this out Android doesn't crash
Wearable.DataApi.putDataItem(client, request).await();
if (result.getStatus().isSuccess()) {
// Do something
Log.e(TAG, "SUCCESS");
} else {
// Do something
}
}
}
TitestModule(模块)
package teunozz.test.module;
import teunozz.test.module.AndroidWearClient;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.TiIntentWrapper;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import android.app.Activity;
import android.content.Intent;
@Kroll.module(name="Titest", id="teunozz.test.module")
public class TitestModule extends KrollModule
{
// Standard Debugging variables
private static final String LCAT = "TitestModule";
private static final boolean DBG = TiConfig.LOGD;
private AndroidWearClient client = new AndroidWearClient();
// You can define constants with @Kroll.constant, for example:
// @Kroll.constant public static final String EXTERNAL_NAME = value;
public TitestModule()
{
super();
}
@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
// put module init code that needs to run when the application is created
}
@Override
public void onDestroy(Activity activity) {
client.stop();
}
@Kroll.method
public void start() {
TiApplication appContext = TiApplication.getInstance();
Activity activity = appContext.getCurrentActivity();
client.start(this);
}
@Kroll.method
public void stop() {
client.stop();
}
@Kroll.method
public void sendData() {
client.sendData();
}
}
基本模块代码和发行版可在GitHub上找到:https://github.com/Teunozz/titest
更新Logcat输出
这是我能找到的第一个错误。
06-20 13:36:06.599: E/AndroidRuntime(14455): *** FATAL EXCEPTION IN SYSTEM PROCESS: NetworkPolicy
06-20 13:36:06.599: E/AndroidRuntime(14455): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED flg=0x40000010 bqHint=4 } in com.android.server.net.NetworkPolicyManagerService$4@bd55cc8
06-20 13:36:06.599: E/AndroidRuntime(14455): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1003)
06-20 13:36:06.599: E/AndroidRuntime(14455): at android.os.Handler.handleCallback(Handler.java:739)
06-20 13:36:06.599: E/AndroidRuntime(14455): at android.os.Handler.dispatchMessage(Handler.java:95)
06-20 13:36:06.599: E/AndroidRuntime(14455): at android.os.Looper.loop(Looper.java:158)
06-20 13:36:06.599: E/AndroidRuntime(14455): at android.os.HandlerThread.run(HandlerThread.java:61)
06-20 13:36:06.599: E/AndroidRuntime(14455): Caused by: java.lang.NullPointerException: Attempt to get length of null array
06-20 13:36:06.599: E/AndroidRuntime(14455): at com.android.server.net.NetworkPolicyManagerService.isUidIdle(NetworkPolicyManagerService.java:3471)
06-20 13:36:06.599: E/AndroidRuntime(14455): at com.android.server.net.NetworkPolicyManagerService.updateRuleForAppIdleLocked(NetworkPolicyManagerService.java:3397)
06-20 13:36:06.599: E/AndroidRuntime(14455): at com.android.server.net.NetworkPolicyManagerService.updateRulesForTempWhitelistChangeLocked(NetworkPolicyManagerService.java:3451)
06-20 13:36:06.599: E/AndroidRuntime(14455): at com.android.server.net.NetworkPolicyManagerService$4.onReceive(NetworkPolicyManagerService.java:731)
06-20 13:36:06.599: E/AndroidRuntime(14455): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:993)
06-20 13:36:06.599: E/AndroidRuntime(14455): ... 4 more
06-20 13:36:06.659: E/android.os.Debug(14455): ro.product_ship = true
06-20 13:36:06.659: E/android.os.Debug(14455): ro.debug_level = 0x4f4c
06-20 13:36:06.659: E/android.os.Debug(14455): sys.mobilecare.preload = false
...
在此之后,每个服务都会消失并重新启动Android。
06-20 13:36:07.049: I/ServiceManager(3046): service 'wifip2p' died
06-20 13:36:07.059: I/ServiceManager(3046): service 'package' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'user' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'activity' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'procstats' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'meminfo' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'gfxinfo' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'dbinfo' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'cpuinfo' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'permission' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'processinfo' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'sensorservice' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'lock_settings' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'mdm.remotedesktop' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'battery' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'usagestats' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'webviewupdate' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'scheduling_policy' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'telephony.registry' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'persona' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'deviceidle' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'application_policy' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'wifi_policy' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'phone_restriction_policy' died
06-20 13:36:07.069: I/ServiceManager(3046): service 'remoteinjection' died
...