广播接收器,用于发送通知并在活动处于活动状态时更新 UI



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

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

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

这是最好的解决方案吗?

有没有办法从broadcastReceiver让我的Activity(如果正在运行)再次恢复?是否可以从broadcastReceiver调用一个Service,如果我的活动正在运行,这将更新我的 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);
    }
}

广播接收机提供活动服务

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

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

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

您可以简单地使用LocalBroadcastReceiver将事件发送到您的活动,可以在此处找到一个工作示例

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

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

最新更新