我在 android studio 中构建了简单的服务,以在控制台日志中每秒运行一个服务,
我想在 react native 中实现我的 android studio 代码
有办法吗?
假设我有一个代码:
myService.class
public class myService extends Service {
private Handler handler= new Handler();
private boolean run = true;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onStart(Intent i, int startId){
super.onStart(i, startId);
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (run){
Log.e("Second", "test");
}
handler.postDelayed(this,1000);
}
},1000);
}
public void onDestroy(){
super.onDestroy();
run=false;
Log.d("Test", "Screen on");
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onPause() {
super.onPause();
Log.d("Test", "Screen off");
startService(new Intent(this, myService.class));
}
@Override
protected void onResume() {
super.onResume();
startService(new Intent(this, myService.class));
}
}