Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自动设置通知_Java_Android_Java Threads - Fatal编程技术网

Java 自动设置通知

Java 自动设置通知,java,android,java-threads,Java,Android,Java Threads,我使用BackgroundThread extends Thread类创建了一个每2小时发送一次通知的应用程序,它工作得很好,但是如果我关闭了应用程序,我没有收到任何通知,你能帮我吗 这是我的密码 类BackgroundThread扩展了线程 以下是主要活动: public class MainActivity extends Activity { BackgroundThread backgroundThread; protected void onStart() {

我使用BackgroundThread extends Thread类创建了一个每2小时发送一次通知的应用程序,它工作得很好,但是如果我关闭了应用程序,我没有收到任何通知,你能帮我吗

这是我的密码

类BackgroundThread扩展了线程

以下是主要活动:

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