每次 BackgroundService 触发时在 Mainactivity 中获取更新
Get updates in Mainactivity every time BackgroundService trigger
我有
public class Backgroundservice implements AlarmReceiverCallback {
private Context context;
public Backgroundservice(Context context) {
this.context = context;
}
@Override
public void onAlarmReceived() {
//interface trigger
}}
现在我想在 MainActivity class 中扩展 Backgroundservice 扩展 AppCompatActivity.
一旦 Backgroundservice 中的接口被触发,我就可以在 mainactivity 中使用我的方法
您可以使用 BroadCastReceiver
为你服务class
public class Backgroundservice implements AlarmReceiverCallback {
private Context context;
public Backgroundservice(Context context) {
this.context = context;
}
@Override
public void onAlarmReceived() {
// send broadcast from here
Intent intent = new Intent("alaram_received");
context.sendBroadcast(intent);
}
并在您的 MainActivity 中接收此广播。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Register Receiver
IntentFilter intentFilter = new IntentFilter("alaram_received");
registerReceiver(alarm_receiver,intentFilter);
}
BroadcastReceiver alarm_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// refresh Token
}
};
}
编码愉快。
我设法通过创建新界面让它工作
public interface ReceiverCallback {
public void onReceived();}
并在我的主要活动中实施,而不是在
中创建它的新对象
@Override
public void onReceive(Context context, Intent intent)
我有
public class Backgroundservice implements AlarmReceiverCallback {
private Context context;
public Backgroundservice(Context context) {
this.context = context;
}
@Override
public void onAlarmReceived() {
//interface trigger
}}
现在我想在 MainActivity class 中扩展 Backgroundservice 扩展 AppCompatActivity.
一旦 Backgroundservice 中的接口被触发,我就可以在 mainactivity 中使用我的方法
您可以使用 BroadCastReceiver
为你服务class
public class Backgroundservice implements AlarmReceiverCallback {
private Context context;
public Backgroundservice(Context context) {
this.context = context;
}
@Override
public void onAlarmReceived() {
// send broadcast from here
Intent intent = new Intent("alaram_received");
context.sendBroadcast(intent);
}
并在您的 MainActivity 中接收此广播。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Register Receiver
IntentFilter intentFilter = new IntentFilter("alaram_received");
registerReceiver(alarm_receiver,intentFilter);
}
BroadcastReceiver alarm_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// refresh Token
}
};
}
编码愉快。
我设法通过创建新界面让它工作
public interface ReceiverCallback {
public void onReceived();}
并在我的主要活动中实施,而不是在
中创建它的新对象 @Override
public void onReceive(Context context, Intent intent)