不会调用BroadcastReceiver的“ onReceive”方法

时间:2019-05-01 15:47:30

标签: java android broadcastreceiver android-pendingintent

我正在设置一个内部BroadcastReceiver,以获取活动转换更新,例如步行,in_Vehicle。但是从未调用onReceive方法。

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()方法


    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()));
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

要调用您的onReceive(),您必须添加这些-

  • 以这种方式将您的BroadcastReceiver添加到清单中-

    <receiver android:name="YourReceiver"> <intent-filter> <action android:name="YourAction" /> </intent-filter> </receiver>

  • 在您的活动中致电registerReceiver()。 另外,不要忘记致电unegisterReceiver()以避免泄漏。

答案 1 :(得分:0)

您必须使用所需的操作在清单中注册Receiver:

   <receiver android:name=".Your_receiver_name" >
        <intent-filter>
            <action android:name="THE_ACTION_NAME"
                    android:exported="false"/>
        </intent-filter>
    </receiver>