gpt4 book ai didi

android - 如何在设备启动时使用 BroadcastReceivers 启动 Google API - ActivityRecognitionAPI?

转载 作者:行者123 更新时间:2023-11-29 15:42:28 24 4
gpt4 key购买 nike

我已经通过 Activities 使用了这个 API,它可以正常工作。我尝试使用此 BroadcastReceiver 启动 MainActivity 并且它有效。但是,当我尝试这样做时,我的应用程序崩溃了。

我需要为 Intent 添加标志吗?在这种情况下该怎么做?

我尝试了这些代码,但我的应用程序在启动时崩溃了:

1) BroadcastReceiver类:

public class startReceiver extends BroadcastReceiver
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

public Context appContext;
public GoogleApiClient mApiClient;

@Override
public void onReceive(Context context, Intent intent) {
appContext = context;

mApiClient = new GoogleApiClient.Builder(context)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) context)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) context)
.build();

mApiClient.connect();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
Intent intent = new Intent(appContext, ActivityRecognizedService.class);
PendingIntent pendingIntent = PendingIntent.getService(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, 10000, pendingIntent);
}

@Override
public void onConnectionSuspended(int i) {
Toast.makeText(appContext, "Connection to Google Services suspended!", Toast.LENGTH_LONG).show();
mApiClient.reconnect();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(appContext, "Connection to Google Services failed!", Toast.LENGTH_LONG).show();
mApiClient.connect();
}

2) ActivityRecognizedService

public class ActivityRecognizedService extends IntentService {

public ActivityRecognizedService() {
super("ActivityRecognizedService");
}

public ActivityRecognizedService(String name) {
super(name);
}

@Override
protected void onHandleIntent(Intent intent) {
if (ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
handleDetectedActivities(result.getProbableActivities());
}
}

private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
for (DetectedActivity activity : probableActivities) {
switch (activity.getType()) {
case DetectedActivity.IN_VEHICLE: {
Log.e("ActivityRecogition", "In Vehicle: " + activity.getConfidence());
break;
}
case DetectedActivity.ON_BICYCLE: {
Log.e("ActivityRecogition", "On Bicycle: " + activity.getConfidence());
break;
}
case DetectedActivity.ON_FOOT: {
Log.e("ActivityRecogition", "On Foot: " + activity.getConfidence());
break;
}
case DetectedActivity.RUNNING: {
Log.e("ActivityRecogition", "Running: " + activity.getConfidence());
break;
}
case DetectedActivity.STILL: {
Log.e("ActivityRecogition", "Still: " + activity.getConfidence());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentText("Are you walking?");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(getString(R.string.app_name));
NotificationManagerCompat.from(this).notify(0, builder.build());
break;
}
case DetectedActivity.TILTING: {
Log.e("ActivityRecogition", "Tilting: " + activity.getConfidence());
break;
}
case DetectedActivity.WALKING: {
Log.e("ActivityRecogition", "Walking: " + activity.getConfidence());
break;
}
case DetectedActivity.UNKNOWN: {
Log.e("ActivityRecogition", "Unknown: " + activity.getConfidence());
break;
}
}
}
}

首先,我使用了此链接中使用的 API:http://code.tutsplus.com/tutorials/how-to-recognize-user-activity-with-activity-recognition--cms-25851

最佳答案

在您的 AdnroidManifest.xml 中添加必要的权限:

<uses-permission android:name = "android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

然后您需要将启动消息与特定的 BroadcastReceiver 链接起来,它将接收并处理手机发出的消息(“启动”)。您可以将您的广播接收器定义为 WakefulBroadcastReceiver 的扩展,以确保设备在您的服务启动之前保持唤醒状态,例如

public class MyBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context con, Intent i) {
Intent intent = new Intent(con, MyIntentService.class);
startWakefulService(con, intent);
}
}

之后,在 list 文件中声明这个接收器:

<receiver android:name="com.example.MyBroadcastReceiver">  
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

BOOT_COMPLETED 消息确保我们的接收器在设备启动时启动。当收到引导消息时,“唤醒”接收器启动服务。我们还需要释放 onHandleIntent 中的唤醒锁,以便设备可以在服务启动后返回 sleep 状态:

public class MyIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent i) {
//Release the wake lock
WakefulBroadcastReceiver.completeWakefulIntent(i);
}
}

现在该服务将在设备启动时自动启动

关于android - 如何在设备启动时使用 BroadcastReceivers 启动 Google API - ActivityRecognitionAPI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38305246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com