BroadcastReceiver发送通知并更新ui如果活动,活动

时间:2015-01-08 11:56:38

标签: android service broadcastreceiver

我有一项只显示每日柜台的活动。当一天结束时,我想发送一个带有最后一天计数器的通知,将其插入数据库中,如果应用程序正在运行,则将计数器标签更新为零。

我想我必须注册静态<receiver>android.intent.action.DATE_CHANGED</receiver>。以便即使活动未运行也会收到通知。 在那里,我在DB中插入值,然后发送通知。

动态创建的广播接收器,我将取消注册onPause,它接收相同的事件,但它只会更新UI,特别是标签。

这是最好的解决方案吗?

broadcastReceiver有什么方法让我的Activity(如果正在运行)再次恢复? 可以从broadcastReceiver调用Service,如果我的活动正在运行,它会更新我的用户界面吗?

3 个答案:

答案 0 :(得分:1)

使用可以捕获事件的broadcastreceiver

public class EventReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(),
                NotificationService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

broadcastreceiver提供活动服务

public class NotificationService extends IntentService {

    private final ServiceBinder binder = new ServiceBinder();
    private ServiceListener serviceListener;

    public NotificationService() {
        super("NotificationService");
    }
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        serviceListener = null;
        return true;
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //send notification
        if(serviceListener != null){
            serviceListener.updateActivity();
        }
    }

    public void setServiceListener(ServiceListener serviceListener) {
        this.serviceListener = serviceListener;
    }

    public class ServiceBinder extends Binder {
        public NotificationService getSevice(){
            return NotificationService.this;
        }
    }

    public static interface ServiceListener{
        public void updateActivity();
    }
}

然后您需要连接到活动中的服务

public class MainActivity extends Activity implements NotificationService.ServiceListener {
    private boolean isBound;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            NotificationService.ServiceBinder binder = ((NotificationService.ServiceBinder)service);
            binder.getSevice().setServiceListener(MainActivity.this);
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!isBound) {
            Intent intent = new Intent(this, NotificationService.class);
            bindService(intent, serviceConnection, BIND_AUTO_CREATE);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(isBound){
          unbindService(serviceConnection);
          isBound = false;
        }
    }

    @Override
    public void updateActivity() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //update you list here
            }
        });
    }
}

并且不要忘记在清单文件中提及service和broadcastreceiver并指定intent filter

<receiver android:name="EventReceiver">
    <intent-filter>
       <action android:name="........." /> 
    </intent-filter>
</receiver>
<service android:name="NotificationService"/>

答案 1 :(得分:0)

您只需使用LocalBroadcastReceiver向您的活动发送活动,就可以找到一个工作示例here

答案 2 :(得分:0)

经过更多阅读后,我发现最佳解决方案是上述。

用于DATE_CHANGED操作的静态广播接收器。这将启动一个服务,它将更新数据库并发送本地自定义广播(com.myBroadcast.WakeUpActivity) 如果正在运行,则活动将为先前的自定义广播注册接收器(将取消注册onPause),并将更新UI。