我已经从可以在我的应用程序上完美运行的“活动识别” API更新到了不幸的是,该API不能使用过渡API。
我要进行更新的原因是要根据Android提出的有关后台流程限制,广播接收器限制以及需要在2018年11月之前定位到SDK 28的应用的新要求来更新代码。
我似乎遇到的问题是广播接收器,尽管它是显式的(因此它不应受到androids对接收器的新限制的影响) 不射击。 我遵循了有关如何正确使用过渡API的教程,并在不同区域更新了我的代码,即从旧服务更改为现在使用新的JobIntentservice,该新的JobIntentservice在我的应用程序的其他部分正常工作。 一切似乎都能正常进行,直到需要发送广播为止,无论我是否不知道,但是我知道我的接收器什么也没收到。
如果有人能够指出问题所在,我将非常感谢。 将我在做什么。我创建了一个可以检测您开车时间的应用程序,一旦执行该程序,它将自动使您的手机处于静音状态,因此您不会被它分心,并且它将向在您通话期间打电话或发短信的任何人发送自动回复短信驾驶。
我在下面发布了一些代码:
在设置活动中,用户可以启用活动过渡APi,一旦激活该活动,应将广播意图发送到ActivityTransitionReceiver类。
SettingsActivity.Java(AppCompatPreferenceActivity)
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Log.i(TAG, "onSharedPreferenceChanged() ");
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final PackageManager packageManager = getApplicationContext().getPackageManager();
final ComponentName ActivityTransitionComponent = new ComponentName(getApplicationContext(), ActivityTransitionReceiver.class);
ActivityTransitions activityTransitions = new ActivityTransitions();
Intent startAcrIntent = new Intent (getApplicationContext(), ActivityTransitionReceiver.class);
startAcrIntent.setAction("westport.andrewirwin.com.drivesafe.ACR");
PendingIntent acrPendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
0,
startAcrIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
if (key.equals("autoStart_switch")) {
Log.i(TAG, key + " changed");
if (prefs.getBoolean("autoStart_switch", false)) {
Log.i(TAG, "autoStart_switch: true");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
//myPendingIntent is the instance of PendingIntent where the app receives callbacks.
// Start Activity Transistion Recognition
packageManager.setComponentEnabledSetting(ActivityTransitionComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Task<Void> task =
ActivityRecognition.getClient(
getApplicationContext()).requestActivityTransitionUpdates(
activityTransitions.getActivityTransitionsRequest(),
acrPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Log.i(TAG, "ACR RUNNING SUCCESS!!");
// packageManager.setComponentEnabledSetting(ActivityTransitionComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
// PackageManager.DONT_KILL_APP);
Log.i(TAG, "ActivityTransitionReceiver Enabled");
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i(TAG, "ACR FAILED TO START ");
packageManager.setComponentEnabledSetting(ActivityTransitionComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
});
//}
} else {
Log.i(TAG, "autoStart_switch: false");
if (prefs.getBoolean("bluetoothAuto_switch", false)) {
return;
}
// Stop Activity Transition Recognition
packageManager.setComponentEnabledSetting(ActivityTransitionComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Task<Void> task =
ActivityRecognition.getClient(
getApplicationContext()).removeActivityTransitionUpdates(acrPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Log.i(TAG, "ACR STOPPED ");
// packageManager.setComponentEnabledSetting(ActivityTransitionComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
// PackageManager.DONT_KILL_APP);
Log.i(TAG, "ActivityTransitionReceiver: Disabled");
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i(TAG, "ACR FAILED TO STOP ");
}
});
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<receiver android:name=".ActivityTransitionReceiver"
android:enabled="false"
android:exported="true">
<intent-filter>
<action android:name="westport.andrewirwin.com.drivesafe.ACR"/>
</intent-filter>
</receiver>
ActivityTransitionReceiver
public class ActivityTransitionReceiver extends BroadcastReceiver {
private String TAG = "ActivityTransitionReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive: ");
}
在此先感谢您的帮助