Java 自动设置通知
我使用BackgroundThread extends Thread类创建了一个每2小时发送一次通知的应用程序,它工作得很好,但是如果我关闭了应用程序,我没有收到任何通知,你能帮我吗 这是我的密码 类BackgroundThread扩展了线程 以下是主要活动:Java 自动设置通知,java,android,java-threads,Java,Android,Java Threads,我使用BackgroundThread extends Thread类创建了一个每2小时发送一次通知的应用程序,它工作得很好,但是如果我关闭了应用程序,我没有收到任何通知,你能帮我吗 这是我的密码 类BackgroundThread扩展了线程 以下是主要活动: public class MainActivity extends Activity { BackgroundThread backgroundThread; protected void onStart() {
public class MainActivity extends Activity {
BackgroundThread backgroundThread;
protected void onStart() {
super.onStart();
backgroundThread = new BackgroundThread(this);
backgroundThread.setRunning(true);
backgroundThread.start();
}
protected void onStop() {
super.onStop();
boolean retry = true;
backgroundThread.setRunning(false);
while (retry) {
try {
backgroundThread.join();
retry = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
我相信你需要把它创造成一种服务而不是一种活动。检查此URL-您需要cron作业。或者像quartzdo这样的调度器,你是说mainActivity扩展了服务
public class MainActivity extends Activity {
BackgroundThread backgroundThread;
protected void onStart() {
super.onStart();
backgroundThread = new BackgroundThread(this);
backgroundThread.setRunning(true);
backgroundThread.start();
}
protected void onStop() {
super.onStop();
boolean retry = true;
backgroundThread.setRunning(false);
while (retry) {
try {
backgroundThread.join();
retry = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}