如果单独运行,Android应用程序不会在真实设备上运行,但如果连接到系统(调试模式或运行模式),则会在模拟器和设备上运行

如果单独运行,Android应用程序不会在真实设备上运行,但如果连接到系统(调试模式或运行模式),则会在模拟器和设备上运行,android,service,handler,runnable,Android,Service,Handler,Runnable,我真的很无助,为什么我的应用程序会这样运行,以及如何找出到底出了什么问题 目标:-在用户登录后在后台运行服务(实际完成登录活动并启动服务),然后根据服务中的某些条件生成不同的活动 问题:-如果我在调试模式或运行模式下在emulator上运行此应用程序,那么它工作正常 如果通过连接电缆在电话上使用调试模式运行它,它会再次正常工作,当我断开电缆并将其从电话上启动时会出现问题,它会跳过所有条件,不会生成活动,我可以看到服务正在运行 测试:-我已经在所有可能的模拟器上测试了它,没有任何失败。在设备上,我

我真的很无助,为什么我的应用程序会这样运行,以及如何找出到底出了什么问题

目标:-在用户登录后在后台运行
服务
(实际完成
登录活动
并启动
服务
),然后根据
服务
中的某些条件生成不同的活动

问题:-如果我在调试模式或运行模式下在emulator上运行此应用程序,那么它工作正常 如果通过连接电缆在电话上使用调试模式运行它,它会再次正常工作,当我断开电缆并将其从电话上启动时会出现问题,它会跳过所有条件,不会生成活动,我可以看到服务正在运行

测试:-我已经在所有可能的模拟器上测试了它,没有任何失败。在设备上,我在
2.3.6姜饼
和4.0.4 IC上测试了它,但在两款手机上都失败了

我使用的是:-

android:minSdkVersion="8"
android:targetSdkVersion="16"
服务 Runnables+处理程序以特定时间间隔从服务打开活动

代码:-

SERVICE.java

public class EgbaService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    ActivityInstances_VO.setApplication(getApplication());
    ActivityInstances_VO.setContext(getBaseContext());

    new EgbaTimerTask();

}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
public class EgbaTimerTask  {

    //private CloseApplicationTask mCloseApplicationTask = null;

    private SQLHelper helper = null;

    public static boolean requestAdvertisementRun =false;
    private boolean requestMaintainApplicationRun =false;

    private long intervalcalculateTimeSpent = 60000;
    private long intervalShowAdvertisements = 0;

    private int TimeSpent = 0 ;

    private Handler handlerShowAdvertisements = new Handler();
    private Handler handlerCalculateTimespent = new Handler();



    Context mycontext;
    Activity myactivity;
    Application myapp;  

    public EgbaTimerTask()
    {
        this.mycontext=ActivityInstances_VO.getContext();
        this.myapp=ActivityInstances_VO.getApplication();
        this.intervalShowAdvertisements=Advertisements_VO.getInterval() * 60000;
        this.requestAdvertisementRun=true;
        this.requestMaintainApplicationRun=true;
        initializeDatabase();
        startTimerTask();

    }

    private void initializeDatabase() {
        // TODO Auto-generated method stub
        helper=new SQLHelper(mycontext);

        try{
            helper.createDataBase();
        }catch (IOException ioe) {
            throw new Error("Unable To Create Database");
        }
        try{
             helper.openDataBase();
        }catch (SQLException sqle) {
            throw sqle;
        }
    }


    public void startTimerTask()
    {
        handlerCalculateTimespent.postDelayed(runnablehandlerCalculateTimespent, intervalcalculateTimeSpent);
        handlerShowAdvertisements.postDelayed(runnableShowAdvertisements, intervalShowAdvertisements);
    }

    private Runnable runnableShowAdvertisements = new Runnable() {
       @Override
       public void run() {
          /* do what you need to do */
           //android.os.Debug.waitForDebugger();

           if(requestAdvertisementRun)
           {


           ShowAdvertisementActivity();
          /* and here comes the "trick" */

            handlerShowAdvertisements.postDelayed(this, intervalShowAdvertisements);
            //Log.i("Who's Running", "Running ShowAdvertisement");

           }
            /* if(i >= 4)
             {   

                 Log.i("Value of i inside check", String.valueOf(i));                 
                 handlerShowAdvertisements.removeCallbacks(runnableShowAdvertisements);
             }*/

       }
    };

    private Runnable runnablehandlerCalculateTimespent = new Runnable() {
           @Override
           public void run() {
              /* do what you need to do */
              /* and here comes the "trick" */

               if(requestMaintainApplicationRun)
               {               

                handlerShowAdvertisements.postDelayed(this, intervalcalculateTimeSpent);
                Log.i("Who's Running", "Calculating Time Spent by User"+String.valueOf(TimeSpent));
                TimeSpent++;
                //android.os.Debug.waitForDebugger();
                if(Registration_VO.getaccount_life()- TimeSpent == 5)
                {
                    Intent i=new Intent(mycontext,NotifyExpirationActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    myapp.startActivity(i);
                    requestAdvertisementRun=false;
                }

                if(Registration_VO.getaccount_life()- TimeSpent == 3)
                {
                    Intent i=new Intent(mycontext,NotifyExpirationActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    myapp.startActivity(i);
                }
                //android.os.Debug.waitForDebugger();
                if(Registration_VO.getaccount_life()- TimeSpent == 0)
                {
                    try
                    {
                        boolean ifSubmitted= false;
                        //check for admin authentication
                        helper.updateUserAccountLifeLeft(Registration_VO.getphoneNo(),0);


                        InternetControl.DisableInternet();
                        stopServicefunction();

                        requestAdvertisementRun=false;
                        requestMaintainApplicationRun=false;
                        handlerShowAdvertisements.removeCallbacks(runnableShowAdvertisements); 
                        handlerCalculateTimespent.removeCallbacks(runnablehandlerCalculateTimespent);


                    }
                    catch(SQLException e)
                    {
                        Log.e("EgbaTimerTask", e.toString());
                    }

                }
               }
           }
        };

        public void stopServicefunction()
        {
            Intent serviceIntent = new Intent();
            serviceIntent.setClass(mycontext, EgbaService.class);
            myapp.stopService(serviceIntent);
        }   


    public void ShowAdvertisementActivity()
    {
        Intent dialogIntent = new Intent(mycontext, ShowAdvertisementActivity.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myapp.startActivity(dialogIntent);

    }
TimerTask.java

public class EgbaService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    ActivityInstances_VO.setApplication(getApplication());
    ActivityInstances_VO.setContext(getBaseContext());

    new EgbaTimerTask();

}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
public class EgbaTimerTask  {

    //private CloseApplicationTask mCloseApplicationTask = null;

    private SQLHelper helper = null;

    public static boolean requestAdvertisementRun =false;
    private boolean requestMaintainApplicationRun =false;

    private long intervalcalculateTimeSpent = 60000;
    private long intervalShowAdvertisements = 0;

    private int TimeSpent = 0 ;

    private Handler handlerShowAdvertisements = new Handler();
    private Handler handlerCalculateTimespent = new Handler();



    Context mycontext;
    Activity myactivity;
    Application myapp;  

    public EgbaTimerTask()
    {
        this.mycontext=ActivityInstances_VO.getContext();
        this.myapp=ActivityInstances_VO.getApplication();
        this.intervalShowAdvertisements=Advertisements_VO.getInterval() * 60000;
        this.requestAdvertisementRun=true;
        this.requestMaintainApplicationRun=true;
        initializeDatabase();
        startTimerTask();

    }

    private void initializeDatabase() {
        // TODO Auto-generated method stub
        helper=new SQLHelper(mycontext);

        try{
            helper.createDataBase();
        }catch (IOException ioe) {
            throw new Error("Unable To Create Database");
        }
        try{
             helper.openDataBase();
        }catch (SQLException sqle) {
            throw sqle;
        }
    }


    public void startTimerTask()
    {
        handlerCalculateTimespent.postDelayed(runnablehandlerCalculateTimespent, intervalcalculateTimeSpent);
        handlerShowAdvertisements.postDelayed(runnableShowAdvertisements, intervalShowAdvertisements);
    }

    private Runnable runnableShowAdvertisements = new Runnable() {
       @Override
       public void run() {
          /* do what you need to do */
           //android.os.Debug.waitForDebugger();

           if(requestAdvertisementRun)
           {


           ShowAdvertisementActivity();
          /* and here comes the "trick" */

            handlerShowAdvertisements.postDelayed(this, intervalShowAdvertisements);
            //Log.i("Who's Running", "Running ShowAdvertisement");

           }
            /* if(i >= 4)
             {   

                 Log.i("Value of i inside check", String.valueOf(i));                 
                 handlerShowAdvertisements.removeCallbacks(runnableShowAdvertisements);
             }*/

       }
    };

    private Runnable runnablehandlerCalculateTimespent = new Runnable() {
           @Override
           public void run() {
              /* do what you need to do */
              /* and here comes the "trick" */

               if(requestMaintainApplicationRun)
               {               

                handlerShowAdvertisements.postDelayed(this, intervalcalculateTimeSpent);
                Log.i("Who's Running", "Calculating Time Spent by User"+String.valueOf(TimeSpent));
                TimeSpent++;
                //android.os.Debug.waitForDebugger();
                if(Registration_VO.getaccount_life()- TimeSpent == 5)
                {
                    Intent i=new Intent(mycontext,NotifyExpirationActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    myapp.startActivity(i);
                    requestAdvertisementRun=false;
                }

                if(Registration_VO.getaccount_life()- TimeSpent == 3)
                {
                    Intent i=new Intent(mycontext,NotifyExpirationActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    myapp.startActivity(i);
                }
                //android.os.Debug.waitForDebugger();
                if(Registration_VO.getaccount_life()- TimeSpent == 0)
                {
                    try
                    {
                        boolean ifSubmitted= false;
                        //check for admin authentication
                        helper.updateUserAccountLifeLeft(Registration_VO.getphoneNo(),0);


                        InternetControl.DisableInternet();
                        stopServicefunction();

                        requestAdvertisementRun=false;
                        requestMaintainApplicationRun=false;
                        handlerShowAdvertisements.removeCallbacks(runnableShowAdvertisements); 
                        handlerCalculateTimespent.removeCallbacks(runnablehandlerCalculateTimespent);


                    }
                    catch(SQLException e)
                    {
                        Log.e("EgbaTimerTask", e.toString());
                    }

                }
               }
           }
        };

        public void stopServicefunction()
        {
            Intent serviceIntent = new Intent();
            serviceIntent.setClass(mycontext, EgbaService.class);
            myapp.stopService(serviceIntent);
        }   


    public void ShowAdvertisementActivity()
    {
        Intent dialogIntent = new Intent(mycontext, ShowAdvertisementActivity.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myapp.startActivity(dialogIntent);

    }
注释

  • runnable在1分钟后运行
  • 这个类中有两个Runnable,它们都根据条件打开不同的活动
  • 服务启动timertask以执行所有必需的操作

只是想知道,你怎么知道它没有运行?我可以在“设置”->“应用”->“运行”中看到服务正在运行。然后你应该添加一些日志信息,并使用设备上的logcat进行检查。如果不检查服务的位置,很难知道为什么不运行。这就是问题所在,为了检查logcat,我必须通过数据电缆将设备连接到我的系统。我也试过,然后它的工作方式应该是这样的。你可以有终端模拟器应用程序的logcat,或playstore的logcat应用程序