您好我正在尝试使用ActivityRecognition API创建一个Android应用。但是当我将它安装在我的手机上时,它会在一些动作后崩溃。
你能帮忙吗?
清单:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TrackerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:exported="false" android:name="TrackIntentService"></service>
<receiver android:name="com.penelope.pepie.trackmovement.TrackBroadcastReceiver"></receiver>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
ActivityGoogleApiTracker的构造函数:
public ActivityGoogleApiTracker(TrackerActivity context){
this.context = context;
connecting = false;
recognitionClient = new GoogleApiClient.Builder(context)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Intent intent = new Intent(context, TrackIntentService.class);
recognitionIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
关联的回叫:
public void onConnected(Bundle connectionHint){
try {
connecting = false;
if (requestUpdates) {
Log.d("api_client", "Connected");
// send request
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(recognitionClient, 100, recognitionIntent);
} else {
Log.d("api_client", "Disconnected");
// cancel updates
ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(recognitionClient, recognitionIntent);
//recognitionClient.removeActivityUpdates(recognitionIntent);
}
}
catch(Exception ex)
{
Toast.makeText(context, "onConnected-exception " + ex.getMessage() + " called..", Toast.LENGTH_LONG).show();
}
}
public class TrackBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ActivityRecognitionResult result = intent.getParcelableExtra(TrackIntentService.RECOGNITION_RESULT);
if (context != null && context.getClass() == TrackerActivity.class)
((TrackerActivity)context).UpdateUI(result);
}
}
意图服务
public class TrackIntentService extends IntentService{
public static final String RECOGNITION_RESULT = "result";
public static final String BROADCAST_UPDATE = "new_update";
public TrackIntentService(){
super("TrackService");
}
/** Called when a new activity detection update is available.
*
*/
@Override
protected void onHandleIntent(Intent intent){
try {
if (ActivityRecognitionResult.hasResult(intent)) {
Log.d("intentService", "got new update!");
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// notify main activity
Intent i = new Intent(BROADCAST_UPDATE);
i.putExtra(RECOGNITION_RESULT, result);
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
manager.sendBroadcast(i);
}
}
catch(Exception ex)
{
}
}
}