0
Я работаю над этим приложением. Он просто не исполнится. Аварии каждый раз. Пожалуйста, помогите.Приложение для обнаружения движения пользователя продолжает сбой
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidrecipes.usermotionactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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:name="com.androidrecipes.usermotionactivity.UserMotionService" />
</application>
</manifest>
UserMotionService.java
package com.androidrecipes.usermotionactivity;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
public class UserMotionService extends IntentService {
private static final String TAG = "UserMotionService";
/*
* Callback interface for detected activity type changes
*/
public interface OnActivityChangedListener{
public void onUserActivityChanged(int bestChoice, int bestConfidence,
ActivityRecognitionResult newActivity);
}
/* Last detected activity type */
private DetectedActivity mLastKnownActivity;
/*
* Marshals requests from the background thread so the callbacks
* can be made on the main (UI) thread.
*/
private CallbackHandler mHandler;
private static class CallbackHandler extends Handler {
/* Callback for activity changes */
private OnActivityChangedListener mCallback;
public void setCallback(OnActivityChangedListener callback) {
mCallback = callback;
}
@Override
public void handleMessage(Message msg) {
if (mCallback != null) {
//Read payload data out of the message and fire callback
ActivityRecognitionResult newActivity = (ActivityRecognitionResult) msg.obj;
mCallback.onUserActivityChanged(msg.arg1, msg.arg2, newActivity);
}
}
}
public UserMotionService() {
//String is used to name the background thread created
super("UserMotionService");
mHandler = new CallbackHandler();
}
public void setOnActivityChangedListener(OnActivityChangedListener listener) {
mHandler.setCallback(listener);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.w(TAG, "Service is stopping...");
}
/*
* Incoming action events from the framework will come
* in here. This is called on a background thread, so
* we can do long processing here if we wish.
*/
@Override
protected void onHandleIntent(Intent intent) {
if (ActivityRecognitionResult.hasResult(intent)) {
//Extract the result from the Intent
ActivityRecognitionResult result =
ActivityRecognitionResult.extractResult(intent);
DetectedActivity activity = result.getMostProbableActivity();
Log.v(TAG, "New User Activity Event");
//If the highest probability is UNKNOWN, but the confidence is low,
// check if another exists and select it instead
if (activity.getType() == DetectedActivity.UNKNOWN
&& activity.getConfidence() < 60
&& result.getProbableActivities().size() > 1) {
//Select the next probable element
activity = result.getProbableActivities().get(1);
}
//On a change in activity, alert the callback
if (mLastKnownActivity == null
|| mLastKnownActivity.getType() != activity.getType()
|| mLastKnownActivity.getConfidence() != activity.getConfidence()) {
//Pass the results to the main thread inside a Message
Message msg = Message.obtain(null,
0, //what
activity.getType(), //arg1
activity.getConfidence(), //arg2
result);
mHandler.sendMessage(msg);
}
mLastKnownActivity = activity;
}
}
/*
* This is called when the Activity wants to bind to the
* service. We have to provide a wrapper around this instance
* to pass it back.
*/
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/*
* This is a simple wrapper that we can pass to the Activity
* to allow it direct access to this service.
*/
private LocalBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public UserMotionService getService() {
return UserMotionService.this;
}
}
/*
* Utility to get a good display name for each state
*/
public static String getActivityName(DetectedActivity activity) {
switch(activity.getType()) {
case DetectedActivity.IN_VEHICLE:
return "Driving";
case DetectedActivity.ON_BICYCLE:
return "Biking";
case DetectedActivity.ON_FOOT:
return "Walking";
case DetectedActivity.STILL:
return "Not Moving";
case DetectedActivity.TILTING:
return "Tilting";
case DetectedActivity.UNKNOWN:
default:
return "No Clue";
}
}
}
MainActivity.java
Logcat:
09-02 10:29:16.635: D/dalvikvm(1867): GC_FOR_ALLOC freed 42K, 4% free 2901K/3008K, paused 151ms, total 152ms
09-02 10:29:16.635: I/dalvikvm-heap(1867): Grow heap (frag case) to 3.500MB for 635808-byte allocation
09-02 10:29:16.755: D/dalvikvm(1867): GC_FOR_ALLOC freed 2K, 4% free 3520K/3632K, paused 117ms, total 117ms
09-02 10:29:17.495: D/AndroidRuntime(1867): Shutting down VM
09-02 10:29:17.535: W/dalvikvm(1867): threadid=1: thread exiting with uncaught exception (group=0xb2d74b20)
09-02 10:29:17.675: E/AndroidRuntime(1867): FATAL EXCEPTION: main
09-02 10:29:17.675: E/AndroidRuntime(1867): Process: com.androidrecipes.usermotionactivity, PID: 1867
09-02 10:29:17.675: E/AndroidRuntime(1867): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidrecipes.usermotionactivity/com.androidrecipes.usermotionactivity.MainActivity}: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.os.Handler.dispatchMessage(Handler.java:102)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.os.Looper.loop(Looper.java:136)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-02 10:29:17.675: E/AndroidRuntime(1867): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 10:29:17.675: E/AndroidRuntime(1867): at java.lang.reflect.Method.invoke(Method.java:515)
09-02 10:29:17.675: E/AndroidRuntime(1867): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-02 10:29:17.675: E/AndroidRuntime(1867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-02 10:29:17.675: E/AndroidRuntime(1867): at dalvik.system.NativeStart.main(Native Method)
09-02 10:29:17.675: E/AndroidRuntime(1867): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
09-02 10:29:17.675: E/AndroidRuntime(1867): at com.google.android.gms.common.GooglePlayServicesUtil.A(Unknown Source)
09-02 10:29:17.675: E/AndroidRuntime(1867): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
09-02 10:29:17.675: E/AndroidRuntime(1867): at com.androidrecipes.usermotionactivity.MainActivity.onCreate(MainActivity.java:69)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.app.Activity.performCreate(Activity.java:5231)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-02 10:29:17.675: E/AndroidRuntime(1867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-02 10:29:17.675: E/AndroidRuntime(1867): ... 11 more
Я включил дополнительное разрешение и ошибок в коде не было. Однако он говорит: «К сожалению, UserMotion остановился». Пожалуйста, помогите.
вы даже читать ошибку? – tyczj
хорошая точка LOL;) – injecteer
Я как бы новичок в этом. Не могли бы вы помочь! –