私のアプリケーションがこのように動作している理由と、実際に何がうまくいかないのかを理解する方法は本当に無力です。
目的:-service
ユーザーがログインした後にバックグラウンドで実行し(実際にを終了しlogin activity
て開始しますservice
)、からのいくつかの条件に基づいてさまざまなアクティビティを生成しservice
ます。
問題:-このアプリケーションをエミュレーターでデバッグモードまたは実行モードで実行する場合、ケーブルを接続してデバッグモードを使用して電話で実行すると、正常に動作します。ケーブルを取り外して電話から起動すると問題が発生します。すべての条件をスキップし、アクティビティを生成しません。サービスが実行されていることを確認できます。
テスト済み:-可能なすべてのエミュレーターで必ずテストしました。私がテストしたデバイス2.3.6 Gingerbread
と4.0.4ICSで、両方の電話で失敗しました
私が使用しているもの:-
android:minSdkVersion="8"
android:targetSdkVersion="16"
特定の時間間隔でサービスからアクティビティを開くためのサービスランナブル+ハンドラー。
コード:-
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();
}
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分後に実行されています。
- このクラスには2つの実行可能ファイルがあり、どちらも条件に応じて異なるアクティビティを開きます
- サービスはtimertaskを開始して、必要なすべてのことを実行します