BroadcastReceiver和Activity之间的通信 – android

我的应用程序中有一个广播接收器,每次用户接到来电时都会被触发.现在,当它发生时,我需要广播接收器来调用特定活动中的特定方法.现在,我尝试将此方法设置为静态,因此可用,但有些东西告诉我这是一个非常糟糕的主意.

因此,我尝试在我的活动中实例化广播接收器而不在我的清单中声明它但问题是 – 当应用程序关闭时,活动不存在,因此我无法调用我的方法.

所以我的问题是 – 当广播接收器被激发时,如何在不使其成为“公共静态”的情况下调用此方法?

这是我的活动代码(我删除了不相关的部分)

package com.silverfix.ringo.activities;

import com.silverfix.ringo.R;
import com.silverfix.ringo.activities.fragments.DataManagerFragment;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class RingtonesActivity extends Activity{

    private DataManagerFragment dataManagerFragment;
    private IntentFilter filter;
    private BroadcastReceiver phoneCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ringtones);
        ActionBar ab = getActionBar();
        ab.setDisplayShowTitleEnabled(false);
        ab.setDisplayHomeAsUpEnabled(true);
        dataManagerFragment = new DataManagerFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(dataManagerFragment, "DataManagerFragment");
        ft.commit();
        filter = new IntentFilter();
        filter.addAction("android.intent.action.PHONE_STATE");
        phoneCall = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                dataManagerFragment.act();
            }
        };
        registerReceiver(phoneCall, filter);
    }
}

解决方法:

你可以使用观察者

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ObservableObject.getInstance().updateValue(intent);
    }
}
public class MainActivity extends Activity implements Observer {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ObservableObject.getInstance().addObserver(this);
    }

    @Override
    public void update(Observable observable, Object data) {            
        Toast.makeText(this, String.valueOf("activity observer " + data), Toast.LENGTH_SHORT).show();
    }
}
public class ObservableObject extends Observable {
    private static ObservableObject instance = new ObservableObject();

    public static ObservableObject getInstance() {
        return instance;
    }

    private ObservableObject() {
    }

    public void updateValue(Object data) {
        synchronized (this) {
            setChanged();
            notifyObservers(data);
        }
    }
}

接收器可以通过清单使用.
ObservableObject – 必须是单例.

上一篇:dubbo报:Waiting server-side response timeout by scan timer.


下一篇:MySQL 报错:Translating SQLException with SQL state '42000', error code '1064', mes