Android ActivityRecognitionApi 工作吗,在 onHandleIntent 上没有收到更新
Does Android ActivityRecognitionApi work , No Updates received on onHandleIntent
伙计们有任何人获得了 ActivityrecognitionAPI 在 Android 中工作。它不提供任何更新。已经尝试了 API 文档,Plenty of Examples。 OnHandleIntent 不会触发。
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
public class ActivityRecognitionIntentService extends IntentService{
ActivityRecognitionResult result;
Intent i;
DetectedActivity mpactivity;
public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntentService");
i = new Intent("ACTIVITY_RECOGNITION_DATA");
}
private String getTypes(int type) {
if(type == DetectedActivity.UNKNOWN)
return "Unknown";
else if(type == DetectedActivity.IN_VEHICLE)
return "In Vehicle";
else if(type == DetectedActivity.ON_BICYCLE)
return "On Bicycle";
else if(type == DetectedActivity.RUNNING)
return "Running";
else if(type == DetectedActivity.ON_FOOT)
return "On Foot";
else if(type == DetectedActivity.STILL)
return "Still";
else if(type == DetectedActivity.TILTING)
return "Tilting";
else if(type == DetectedActivity.WALKING)
return "Walking";
else
return "";
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent.getAction() == "ActivityRecognitionIntentService") {
if(ActivityRecognitionResult.hasResult(intent)){
result = ActivityRecognitionResult.extractResult(intent);
mpactivity = result.getMostProbableActivity();
i.putExtra("Activity", getTypes(mpactivity.getType()));
i.putExtra("Confidence", mpactivity.getConfidence());
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}
}
}
主要 activity 与
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.TextView;
import android.widget.Toast;
public class Actrecogex extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
TextView textView1;
GoogleApiClient mGoogleActclient;
PendingIntent mActivityRecognitionPendingIntent;
Intent i;
LocalBroadcastManager mBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actrecogex);
textView1 = new TextView(this);
textView1 =(TextView)findViewById(R.id.textView1);
i = new Intent(this, ActivityRecognitionIntentService.class);
mBroadcastManager = LocalBroadcastManager.getInstance(this);
mGoogleActclient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleActclient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
textView1.setText("Failed Connection" + arg0);
}
@Override
public void onConnected(Bundle arg0) {
i.setAction("ActivityRecognitionIntentService");
mActivityRecognitionPendingIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleActclient, 0, mActivityRecognitionPendingIntent);
}
@Override
public void onConnectionSuspended(int arg0) {
textView1.setText("Failed Suspended" + arg0);
}
private BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Service", Toast.LENGTH_SHORT).show();
String v = "Activity :" + intent.getStringExtra("Activity") + " " + "Confidence : " + intent.getExtras().getInt("Confidence") + "\n";
v += textView1.getText() ;
textView1.setText(v);
}
};
@Override
public void onDisconnected() {
textView1.setText("Disconnected" );
}
}
清单
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<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=".Actrecogex"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<service android:name="ActivityRecognitionIntentService" android:exported="false"></service>
</application>
代码真的有效吗,或者我在浪费时间。真的很想回去Activityrecogntionclient。在 Gingerbread 和 Kitkat 上测试应用程序。双方都不让步
您在清单中对服务的定义似乎是错误的:它缺少前导句点。应该是
<service android:name=".ActivityRecognitionIntentService" android:exported="false"></service>
您的onHandleIntent()
使用
intent.getAction() == "ActivityRecognitionIntentService"
但是您不能将字符串与 ==
进行比较,因此您的 if
语句永远不会 returns 为真。相反,您必须使用 equals()
或等效的 TextUtils.equals()(它也处理任一参数为 null
的情况):
"ActivityRecognitionIntentService".equals(intent.getAction())
// OR
TextUtils.equals(intent.getAction(), "ActivityRecognitionIntentService")
if (intent.getAction() == "ActivityRecognitionIntentService") {
}
给出的是空值。所以周期性的错误信息。
主要问题是 Broadcastreceiver 没有收到任何东西。因此需要为 Broadcastreceiver 单独设置 class。现在正在工作。
<receiver android:name="ActBreceiver" android:exported="false">
<intent-filter>
<action android:name="ACTIVITY_RECOGNITION_DATA"/>
</intent-filter>
</receiver>
伙计们有任何人获得了 ActivityrecognitionAPI 在 Android 中工作。它不提供任何更新。已经尝试了 API 文档,Plenty of Examples。 OnHandleIntent 不会触发。
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
public class ActivityRecognitionIntentService extends IntentService{
ActivityRecognitionResult result;
Intent i;
DetectedActivity mpactivity;
public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntentService");
i = new Intent("ACTIVITY_RECOGNITION_DATA");
}
private String getTypes(int type) {
if(type == DetectedActivity.UNKNOWN)
return "Unknown";
else if(type == DetectedActivity.IN_VEHICLE)
return "In Vehicle";
else if(type == DetectedActivity.ON_BICYCLE)
return "On Bicycle";
else if(type == DetectedActivity.RUNNING)
return "Running";
else if(type == DetectedActivity.ON_FOOT)
return "On Foot";
else if(type == DetectedActivity.STILL)
return "Still";
else if(type == DetectedActivity.TILTING)
return "Tilting";
else if(type == DetectedActivity.WALKING)
return "Walking";
else
return "";
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent.getAction() == "ActivityRecognitionIntentService") {
if(ActivityRecognitionResult.hasResult(intent)){
result = ActivityRecognitionResult.extractResult(intent);
mpactivity = result.getMostProbableActivity();
i.putExtra("Activity", getTypes(mpactivity.getType()));
i.putExtra("Confidence", mpactivity.getConfidence());
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}
}
}
主要 activity 与
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.TextView;
import android.widget.Toast;
public class Actrecogex extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
TextView textView1;
GoogleApiClient mGoogleActclient;
PendingIntent mActivityRecognitionPendingIntent;
Intent i;
LocalBroadcastManager mBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actrecogex);
textView1 = new TextView(this);
textView1 =(TextView)findViewById(R.id.textView1);
i = new Intent(this, ActivityRecognitionIntentService.class);
mBroadcastManager = LocalBroadcastManager.getInstance(this);
mGoogleActclient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleActclient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
textView1.setText("Failed Connection" + arg0);
}
@Override
public void onConnected(Bundle arg0) {
i.setAction("ActivityRecognitionIntentService");
mActivityRecognitionPendingIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleActclient, 0, mActivityRecognitionPendingIntent);
}
@Override
public void onConnectionSuspended(int arg0) {
textView1.setText("Failed Suspended" + arg0);
}
private BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Service", Toast.LENGTH_SHORT).show();
String v = "Activity :" + intent.getStringExtra("Activity") + " " + "Confidence : " + intent.getExtras().getInt("Confidence") + "\n";
v += textView1.getText() ;
textView1.setText(v);
}
};
@Override
public void onDisconnected() {
textView1.setText("Disconnected" );
}
}
清单
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<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=".Actrecogex"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<service android:name="ActivityRecognitionIntentService" android:exported="false"></service>
</application>
代码真的有效吗,或者我在浪费时间。真的很想回去Activityrecogntionclient。在 Gingerbread 和 Kitkat 上测试应用程序。双方都不让步
您在清单中对服务的定义似乎是错误的:它缺少前导句点。应该是
<service android:name=".ActivityRecognitionIntentService" android:exported="false"></service>
您的onHandleIntent()
使用
intent.getAction() == "ActivityRecognitionIntentService"
但是您不能将字符串与 ==
进行比较,因此您的 if
语句永远不会 returns 为真。相反,您必须使用 equals()
或等效的 TextUtils.equals()(它也处理任一参数为 null
的情况):
"ActivityRecognitionIntentService".equals(intent.getAction())
// OR
TextUtils.equals(intent.getAction(), "ActivityRecognitionIntentService")
if (intent.getAction() == "ActivityRecognitionIntentService") {
}
给出的是空值。所以周期性的错误信息。
主要问题是 Broadcastreceiver 没有收到任何东西。因此需要为 Broadcastreceiver 单独设置 class。现在正在工作。
<receiver android:name="ActBreceiver" android:exported="false">
<intent-filter>
<action android:name="ACTIVITY_RECOGNITION_DATA"/>
</intent-filter>
</receiver>