Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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_Service_Alarmmanager - Fatal编程技术网

Java 报警管理器赢得';应用程序关闭时无法启动

Java 报警管理器赢得';应用程序关闭时无法启动,java,android,service,alarmmanager,Java,Android,Service,Alarmmanager,我已将alarm manager实现到我的应用程序中,但感到困惑,因为当我的应用程序关闭/死机时,我的alarm manager不会启动 我在谷歌搜索了很多建议,但没有一个建议有效。 这是我的私事 打开应用程序->自动启动服务/报警管理器 当应用程序每10分钟打开一次时,应用程序将检查服务器以下载数据并插入数据库 当应用程序每10分钟关闭一次时,应用程序将检查服务器以下载数据并插入数据库 问题是当应用程序关闭时,服务也会停止。 这是我的示例代码 MainActivity.java AlarmRe

我已将alarm manager实现到我的应用程序中,但感到困惑,因为当我的应用程序关闭/死机时,我的alarm manager不会启动 我在谷歌搜索了很多建议,但没有一个建议有效。 这是我的私事

  • 打开应用程序->自动启动服务/报警管理器
  • 当应用程序每10分钟打开一次时,应用程序将检查服务器以下载数据并插入数据库
  • 当应用程序每10分钟关闭一次时,应用程序将检查服务器以下载数据并插入数据库
  • 问题是当应用程序关闭时,服务也会停止。 这是我的示例代码

    MainActivity.java

    AlarmReceiver alarm = new AlarmReceiver();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        alarm.setAlarm(this);
    }
    
    AlarmReceiver.Java

    public class AlarmReceiver extends WakefulBroadcastReceiver {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    
    @Override
    public void onReceive(Context context, Intent intent) {   
    
        Intent service = new Intent(context, SchedulingService.class);
        startWakefulService(context, service);
    }
    
    public void setAlarm(Context context) {
        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    
    
         alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                  10000,
                  10000, alarmIntent);
    
        ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
        PackageManager pm = context.getPackageManager();
    
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);           
    }
    
    public void cancelAlarm(Context context) {
        // If the alarm has been set, cancel it.
        if (alarmMgr!= null) {
            alarmMgr.cancel(alarmIntent);
        }
    
    
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
    
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    } }
    
    BootReceiver.java

    public class BootReceiver extends BroadcastReceiver {
    AlarmReceiver alarm = new AlarmReceiver();
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            alarm.setAlarm(context);
        }
    }}
    
    ScheduleService.java

    public class SchedulingService extends IntentService {
    public SchedulingService() {
        super("SchedulingService");
    }
    
    public static final String TAG = "Scheduling Demo";
    public static final int NOTIFICATION_ID = 1;
    public static final String SEARCH_STRING = "Active";
    public static final String URL = "http://localhost/TMALive";
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    
    @Override
    protected void onHandleIntent(Intent intent) {
        String urlString = URL;
        String result ="";
        try {
            result = loadFromNetwork(urlString);
        } catch (IOException e) {
            Log.i(TAG, getString(R.string.connection_error));
        }
    
        if (result.indexOf(SEARCH_STRING) != -1) {
            sendNotification(getString(R.string.live_found));
            Log.i(TAG, "Your Post Live!!");
        } else {
            sendNotification(getString(R.string.no_live));
            Log.i(TAG, "Your Post Off. :-(");
        }
        AlarmReceiver.completeWakefulIntent(intent);
    
    }
    
    
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
               this.getSystemService(Context.NOTIFICATION_SERVICE);
    
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);
    
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getString(R.string.pos_alert))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);
    
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
    private String loadFromNetwork(String urlString) throws IOException {
        InputStream stream = null;
        String str ="";
    
        try {
            stream = downloadUrl(urlString);
            str = readIt(stream);
        } finally {
            if (stream != null) {
                stream.close();
            }      
        }
        return str;
    }
    
    
    private InputStream downloadUrl(String urlString) throws IOException {
    
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Start the query
        conn.connect();
        InputStream stream = conn.getInputStream();
        return stream;
    }
    
    
    private String readIt(InputStream stream) throws IOException {
    
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        for(String line = reader.readLine(); line != null; line = reader.readLine()) 
            builder.append(line);
        reader.close();
        return builder.toString();
    }}
    
    这段代码在应用程序打开时可以正常工作,但在应用程序关闭时没有显示通知,并且在重新启动后也可以正常工作

    你能给我一些建议,使用这种方法还是使用服务方法更好


    非常感谢

    也有同样的问题,在我的情况下,这是因为我测试的设备(华硕手机)上有一个名为“自动启动管理器”的库存应用程序,除非用户明确允许,否则它会阻止应用程序在后台启动。(没有任何警告)


    虽然可能性很小,但如果你按照书中的说明做了所有事情,但在应用程序运行时仍然无法正常工作,那么你可能需要检查你的系统上是否运行了类似的软件。

    也有同样的问题,而在我的情况下,这是因为设备(华硕手机)上有一款名为“自动启动管理器”的股票应用程序我正在测试的,它阻止应用程序在后台启动,除非用户明确允许。(没有任何警告)


    尽管可能性很小,但如果您按照书中的说明做了所有事情,但在应用程序运行时仍然无法运行,则可能需要检查您的系统上是否正在运行类似的软件。

    每当应用程序关闭或从最近的应用程序中删除时,它都会调用
    ontrimmory()
    方法

    当应用程序关闭或从最近的应用程序中删除时,您需要再次设置报警管理器。 在扩展
    应用程序的类中重写以下方法

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        // set alarm here 
    }
    

    每当应用关闭或从最近的应用中删除时,它都会调用
    ontrimmory()
    方法

    当应用程序关闭或从最近的应用程序中删除时,您需要再次设置报警管理器。 在扩展
    应用程序的类中重写以下方法

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        // set alarm here 
    }
    

    我照医生说的做了。到目前为止,它运行得很好。即使手机重新启动或应用程序未运行。一些魅族手机有自我能量管理器,可以在手机关闭屏幕时破坏您的闹钟,这意味着更好的使用服务?但是,如果使用服务,如何每10分钟生成一次服务触发器数据?好吧,我按照这里的文档所说的做了:。到目前为止,它运行得很好。即使手机重新启动或应用程序未运行。一些魅族手机有自我能量管理器,可以在手机关闭屏幕时破坏您的闹钟,这意味着更好的使用服务?但是如果使用服务,如何每10分钟生成一次服务触发器数据?