我有
public class Backgroundservice implements AlarmReceiverCallback {
private Context context;
public Backgroundservice(Context context) {
this.context = context;
}
@Override
public void onAlarmReceived() {
//interface trigger
}}
现在我想在我的 MainActivity 类中扩展 Backgroundservice ,该类扩展了 AppCompatActivity 。
因此,一旦Backgroundservice中的接口被触发,我就可以在mainactivity中使用我的方法
答案 0 :(得分:1)
您可以使用BroadCastReceiver
在您的服务班上
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
}
};
}
快乐编码。
答案 1 :(得分:0)
我希望通过创建新界面使其工作
public interface ReceiverCallback {
public void onReceived();}
并在我的mainactivity中实现,然后在
中创建它的新对象 @Override
public void onReceive(Context context, Intent intent)