- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经将这段代码放在一起来获取用户 Activity ,看看他是走路还是开车还是静止不动,但它不起作用,onHandleIntent
从未调用过。它正在连接到 GoogleApiClient
。
这是我的代码
Activity 布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/msg" />
主 Activity
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;
public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private Context mContext;
private GoogleApiClient mGApiClient;
private BroadcastReceiver receiver;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the textview
textView = (TextView) findViewById(R.id.msg);
//Set the context
mContext = this;
//Check Google Play Service Available
if(isPlayServiceAvailable()) {
mGApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
//Connect to gPlay
mGApiClient.connect();
}else{
Toast.makeText(mContext, "Google Play Service not Available", Toast.LENGTH_LONG).show();
}
//Broadcast receiver
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String v = "Activity :" +
intent.getStringExtra("act") + " " +
"Confidence : " + intent.getExtras().getInt("confidence") + "n";
v += textView.getText();
textView.setText(v);
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("SAVVY");
registerReceiver(receiver, filter);
}
private boolean isPlayServiceAvailable() {
return GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS;
}
@Override
public void onConnected(Bundle bundle) {
Intent i = new Intent(this, ActivityRecognitionIntentService.class);
PendingIntent mActivityRecongPendingIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("Saquib", "connected to ActRecog " + "PI " +mActivityRecongPendingIntent.toString() );
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGApiClient, 0, mActivityRecongPendingIntent);
}
@Override
public void onConnectionSuspended(int i) {
Log.d("Saquib", "suspended to ActRecog");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("Saquib", "Not connected to ActRecog");
}
@Override
protected void onDestroy() {
super.onDestroy();
mGApiClient.disconnect();
unregisterReceiver(receiver);
}
}
ActivityRecognitionIntentService
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
/**
* Created by tutsberry on 17/03/15.
*/
public class ActivityRecognitionIntentService extends IntentService {
public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
if(ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity detectedActivity = result.getMostProbableActivity();
int confidence = detectedActivity.getConfidence();
String mostProbableName = getActivityName(detectedActivity.getType());
Intent i = new Intent("SAVVY");
i.putExtra("act", mostProbableName);
i.putExtra("confidence", confidence);
Log.d("Saquib", "mostProbableName " + mostProbableName);
Log.d("Saquib", "Confidence : " + confidence);
//Send Broadcast
this.sendBroadcast(i);
}else {
Log.d("Saquib", "Intent had no data returned");
}
}
private String getActivityName(int type) {
switch (type)
{
case DetectedActivity.IN_VEHICLE:
return "in_vehicle";
case DetectedActivity.ON_BICYCLE:
return "on_bicycle";
case DetectedActivity.WALKING:
return "walking";
case DetectedActivity.STILL:
return "still";
case DetectedActivity.TILTING:
return "tilting";
case DetectedActivity.UNKNOWN:
return "unknown";
case DetectedActivity.RUNNING:
return "running";
}
return "n/a";
}
}
并体现
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutsberry.moveyours" >
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/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=".ActivityRecognitionIntentService">
</service>
</application>
</manifest>
伙计们请帮忙,谷歌文档对 ActivityRecognition 没有用
最佳答案
也许你错过了
<uses-permission android:name="android.permission.INTERNET" />
关于android ActivityRecognition 不调用 onHandleIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29113919/
我正在尝试使用 Play Services Location API 监听位置更改。我想做的是采用后台应用程序方法并使用 PendingIntent 获取更新。但是,onHandleIntent()
在我正在开发的应用程序中,我正在尝试使用 IntentService,如下面的代码所示。 IntentService 在 list 文件中的声明如下所示。我现在面临的问题是,当我运行 App 时,永远
你得帮帮我。我已经用了几天了,我不知道问题出在哪里,而且已经在生产中了。 我有一个非常简单的 IntentService,它运行良好并且可以将非常大的字符串(Base64 中的图像)上传到服务器
我正在尝试使用 , 为 android 实现 GCM, https://developers.google.com/cloud-messaging/android/client .. Created
我正在开发带有今年发布的地理围栏的 android 应用程序。 我的理解是当用户进入(或离开)围栏时它应该触发 onHandleIntent 方法但是我很难触发到 onHandleIntent 方法
我有两个文件。 IntentServiceTest1Activity.java public class IntentServiceTest1Activity extends Activity {
当我启动服务 (IntentService) 时,它会调用服务函数 onHandleIntent()。在它运行时,如果我再次调用 startService(),它会缓存调用,并再次调用 onHandl
我正在尝试为我的应用获取 Activity 识别更新,但它似乎只能部分工作。一开始我以为它不工作但每隔一段时间调试日志就会显示一个 Activity 类型,这意味着 ActivityRecogniti
我知道以前有人问过这个问题,但我已经遍历了所有我能找到的答案,但仍然无法解决问题。 问题是当 BroadcastReceiver 启动时,IntentService onHandleIntent()
我们应该从 IntentService 的 onHandleIntent() 方法中启动异步任务吗?我读到 onHandleIntent() 在 worker thread 中运行,所以从那里启动 a
我已经将这段代码放在一起来获取用户 Activity ,看看他是走路还是开车还是静止不动,但它不起作用,onHandleIntent 从未调用过。它正在连接到 GoogleApiClient。 这是我
经过几个小时的研究,我终于咨询了官方帮助。为什么 onHandleIntent() 没有被调用?这里有什么问题吗? 在主要 Activity onCreate() 中: mService = new
设备关闭时 onHandleIntent 的行为是什么? 我知道在 IntentService 上,只要 onHandleIntent 没有完成它的工作,服务就会一直运行。 所以考虑一下这是一个关于设
这个问题在这里已经有了答案: IntentService won't start using AlarmManager (3 个答案) 关闭 6 年前。 我希望 IntentService 每 10
我正在关注来自 Android 开发人员的 GCM 演示。遇到以下情况。我在 GCMIntentService 中的 OnHandleIntent 没有被调用。有人可以帮忙吗? package com
我目前正在编写一个需要 IntentService 的 android 程序。当我将代码放入 onHandleIntent 函数时,代码不会运行,但它不会在 MainActivity 中给出错误。但是
我的 android 应用程序崩溃了,这是 logcat:- java.lang.NullPointerException at com.google.android.gcm.GCMBaseI
我正在使用 android xml rpc 来安装服务器。为此,我正在使用和 intentService。唯一的问题是,当服务器类启动时,我的包含服务器的 onHandleIntent 永远不会被调用
我是 Android 工作新手,我不确定是我把自己逼入了困境,还是只是没有看到解决方案(两者都有可能)。我有一个 Intent 服务,它根据用户保存的时间设置重复警报。当警报响起时,其目的是显示一条通
我正在构建一个应用程序,它有一个小部件,小部件中的一个按钮启动了一个 IntentService。onHandleIntent() 运行一些代码,然后通过处理程序引发 toast 。单击小部件中的按钮
我是一名优秀的程序员,十分优秀!