自动启动应用程序不能正常工作 [英] AutoStart Application not working properly

查看:121
本文介绍了自动启动应用程序不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的自动启动应用程序与的TimerTask 的实施,工作正常,几乎很多设备。问题是,它不工作三星Galaxy Y(2.3.6)戴尔XCD35(2.2)。当设备启动的TimerTask 工作几秒钟,然后关闭。我检查了应用程序 - >管理应用程序,我看到的applcation已经在强制停止状态。这意味着我的申请得到了一些如何在几秒钟后停止。那么,究竟是什么怪异的行为在这两个设备的原因,如果任何人有解决方案做分享。

下面是我的code。

MyReceiver.java

 公共类MyReceiver扩展的BroadcastReceiver {

    私人定时器mTimer =新的Timer();
    @覆盖
    公共无效的onReceive(上下文的背景下,意图ARG1){
        Toast.makeText(上下文,设备引导的,Toast.LENGTH_LONG).show();
        Log.d(TAG,设备引导的);
        mTimer.scheduleAtFixedRate(新MyTimerTask(),2000,2000);
    }

    私有类MyTimerTask扩展的TimerTask
    {
        @覆盖
        公共无效的run(){
            Log.d(TAG,TimerTask的执行......);
        }
    }
}
 

AndroidManifest.xml中

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.autostart.app
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-SDK安卓的minSdkVersion =8/>
    <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_BOOT_COMPLETED/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <接收机器人:MyReceiverNAME =>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
            &所述; /意图滤光器>
        < /接收器>
    < /用途>
< /舱单>
 

解决方案

我觉得在某些安卓 OS 有时操作系统杀死正在运行的线程,而设备 靴子其中Android是不熟悉或不识别它。这就是为什么的TimerTask 工作在某些设备在某些设备只适用于5至10秒钟,然后应用程序被 ForceStopped 自动由Android操作系统上的设备 启动。(注意 - 它的力量停止从管理应用程序,而不是强制关闭,所以我没有得到任何错误,在logcat中的)

因此​​,在这种情况下,解决办法是使用内置 机制其中 Android的 OS 识别并不能杀死它,保持它的运行模式。在这种情况下,我设法使用 AlarmManager 来完成我的任务和它的作品。

我可能不是正确的,但我的最终解决方案是使用 AlarmManager 来使每一个设备我的申请工作。

  @覆盖
    公共无效的onReceive(上下文的背景下,意图ARG1){

        意图myIntent =新的意图(背景下,AlarmService.class);
        PendingIntent pendingIntent = PendingIntent。
                                         的getService(上下文,0,myIntent,0);
        AlarmManager alarmManager =(AlarmManager)上下文
                                    .getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                      System.currentTimeMillis的()+ 2000,2000,pendingIntent);
    }
 

更新:

<一个href="http://stackoverflow.com/questions/6558694/difference-between-alarmmanager-and-scheduledexecutorservice"><$c$c>AlaramManager是运行所有的时间关键的系统服务。

I am having a Simple AutoStart Application with TimerTask implementation, that works fine in almost many devices. The problem is that it is not working in Samsung Galaxy Y(2.3.6) and DELL XCD35(2.2). When the device boots TimerTask works for some seconds and then shuts down. I check in the Application->Manage Application, I saw that the Applcation was already in Force Stop State. That means some how my Application gets stopped after some seconds. So, what is the reason for this weird behaviour in these two devices, if anyone has the solution do share it.

Below is my code.

MyReceiver.java

public class MyReceiver extends BroadcastReceiver{

    private Timer mTimer = new Timer();
    @Override
    public void onReceive(Context context, Intent arg1) {
        Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
        Log.d("TAG","Device Booted");
        mTimer.scheduleAtFixedRate(new MyTimerTask(), 2000,2000);
    }

    private class MyTimerTask extends TimerTask
    {
        @Override
        public void run() {
            Log.d("TAG","TimerTask executed....");
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.autostart.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

解决方案

I think in some of the Android OS sometimes the OS kills the threads that are running while the Devices Boots which Android is not familiar with or doesn't recognize it. This is the reason why the TimerTask is working in some Devices and in some Devices just works for a 5-10 seconds and then the Application is ForceStopped automatically by the Android OS on Device Boot(Note - Its Force Stop from Manage Application and not Force close so I am not getting any error in the Logcat).

So in that case the solution is to use the inbuilt Mechanism which Android OS recognizes and doesn't kill it and keeps it in a running mode. In this case I managed using AlarmManager to perform my task and it works.

I might not be right but my final solution was to use AlarmManager to make my Application working in every Device.

@Override
    public void onReceive(Context context, Intent arg1) {

        Intent myIntent = new Intent(context, AlarmService.class);
        PendingIntent pendingIntent = PendingIntent.
                                         getService(context, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) context
                                    .getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                      System.currentTimeMillis() + 2000, 2000, pendingIntent);
    }

UPDATE:

AlaramManager is critical system service that runs all the time.

这篇关于自动启动应用程序不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆