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

BroadcastReceiver to send notification and update ui if activity, active

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

我想我必须注册一个静态 <receiver>android.intent.action.DATE_CHANGED</receiver>。以便即使 activity 未运行也能收到通知。 我在数据库中插入值并发送通知。

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

这是最好的解决方案吗?

broadcastReceiver 有什么方法可以让我的 Activity(如果正在运行)再次恢复吗? 是否可以从 broadcastReceiver 调用 Service,如果我的 Activity 正在运行,它将更新我的 UI?

使用可以捕获事件的广播接收器

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

然后您需要连接到 activity

中的服务
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
            }
        });
    }
}

并且不要忘记在清单文件中提及服务和广播接收器并指定意图过滤器

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

您可以简单地使用 LocalBroadcastReceiver to send events to your Activity , a working example could be found here

经过更多阅读,我发现最好的解决方案是上面的。

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