I'm setting up an inner BroadcastReceiver
to get activity transitions Updates such as walking, in_Vehicle. But the onReceive
method is never called.
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = new Intent(TRANSITIONS_RECEIVER_ACTION);
mPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
mTransitionsReceiver = new TransitionsReceiver();
registerReceiver(mTransitionsReceiver, new IntentFilter(TRANSITIONS_RECEIVER_ACTION));
mLogFragment = (LogFragment) getSupportFragmentManager().findFragmentById(R.id.log_fragment);
}
setupActivityTransitions();
setupActivityTransitions()
method
ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);
// Register for Transitions Updates.
Task<Void> task =
ActivityRecognition.getClient(this)
.requestActivityTransitionUpdates(request, mPendingIntent);
task.addOnSuccessListener(
new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Log.i(TAG, "Transitions Api was successfully registered.");
}
});
task.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, "Transitions Api could not be registered: " + e);
}
});
BroadcastReceiver
public class TransitionsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("@2345","dfhudfdf");
if (!TextUtils.equals(TRANSITIONS_RECEIVER_ACTION, intent.getAction())) {
mLogFragment.getLogView()
.println("Received an unsupported action in TransitionsReceiver: action="
+ intent.getAction());
return;
}
if (ActivityTransitionResult.hasResult(intent)) {
ActivityTransitionResult result = ActivityTransitionResult.extractResult(intent);
for (ActivityTransitionEvent event : result.getTransitionEvents()) {
String activity = toActivityString(event.getActivityType());
String transitionType = toTransitionType(event.getTransitionType());
mLogFragment.getLogView()
.println("Transition: "
+ activity + " (" + transitionType + ")" + " "
+ new SimpleDateFormat("HH:mm:ss", Locale.US)
.format(new Date()));
}
}
}
}
For getting your onReceive()
called, you will have to add these -
add your BroadcastReceiver to into manifest , in this way -
<receiver android:name="YourReceiver"> <intent-filter> <action android:name="YourAction" /> </intent-filter> </receiver>
call registerReceiver()
in your activity. Also, don't forget to call unegisterReceiver()
to avoid leaks.
you have to register you Receiver in the manifest with the action you want :
<receiver android:name=".Your_receiver_name" >
<intent-filter>
<action android:name="THE_ACTION_NAME"
android:exported="false"/>
</intent-filter>
</receiver>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.