我真的很无奈,为什么我的应用程序会这样,以及如何弄清楚到底出了什么问题。
AIM :-service
在用户登录后在后台运行(实际上完成login activity
并启动 a service
),然后根据某些条件从service
.
问题:-如果我在调试模式或运行模式下在模拟器上运行此应用程序,那么如果通过连接电缆使用调试模式在手机上运行它,它会再次正常工作,当我断开电缆并从手机上启动它时会出现问题跳过所有条件并且不生成活动,我可以看到服务正在运行。
测试:-我已经在所有可能的模拟器上测试了它,没有任何失败。在我已经在4.0.4 ICS2.3.6 Gingerbread
上测试过的设备上,它在两部手机上都失败了
我在用什么:-
android:minSdkVersion="8"
android:targetSdkVersion="16"
服务 Runnables + Handlers 在特定时间间隔从服务中打开活动..
代码 :-
服务.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();
}
TimerTask.java
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,都根据条件打开不同的activity
- 服务启动 timertask 来做所有需要的事情