2011-11-14 2 views
0

を再開:サービスAlarmManagerが自動に停止して5秒ごとに+を開始し、それは私の非常に単純なコードです

アプリケーション:

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 
public Intent myIntent; 
public Button OnButton; 
public Button OffButton; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    myIntent = new Intent(this, TimerService.class); 

    OnButton = (Button) findViewById(R.id.onbutton); 
    OffButton = (Button) findViewById(R.id.offbutton); 

    OnButton.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      startService(myIntent); 
     } 
    }); 

    OffButton.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      stopService(myIntent); 
     } 
    }); 
    } 
} 

サービス:

public class TimerService extends Service 
{ 
public AlarmManager Alarmmgr; 
public PendingIntent myPendingIntent; 

@Override 
public void onCreate() 
{ 
    Alarmmgr = (AlarmManager) getSystemService(ALARM_SERVICE); 
} 

public void SimpleMethod() 
{ 
     //Here I will do something 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    SimpleMethod(); 

    myPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    Alarmmgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, myPendingIntent); 

    return Service.START_NOT_STICKY; 
} 

@Override 
public IBinder onBind(Intent intent) 
{ 
    return null; 
} 


@Override 
public void onDestroy() 
{ 
    Alarmmgr.cancel(myPendingIntent); 
} 

} 

問題は、私が起動したときにということであり、私のサービスとそれが30-50分のために働いている画面をオフにします。その時間の後、それは10〜50分間停止し、再び再開します...私は休憩せずに連続して実行したいです。私のコードで変更する必要があるのは何ですか?私はタイマーを試しましたが、画面をオフにしても機能しません。

+0

[前景](http://developer.android.com/reference/android/app/Service.html) – yorkw

答えて

0

おそらく、携帯電話がスリープモードに入るのを防ぐ必要がありますか?

+0

私の画面などをオフにしないようにすることはできませんか?私はなぜ、他のアプリケーション(私ではない)がバックグラウンドで動作し、例えば5分ごとにファイルを保存できるのか、不思議です。インターバルが小さすぎるかもしれませんか?奇妙な。 – krzysnick

+0

wakelockを追加しましたが、私のサービスが時折眠っています。私は電話が寝ていても、サービスは常にバックグラウンドで実行されていると思っていましたが、今はその効果を得るのが難しいですね... – krzysnick

+0

毎回アラームを設定するのではなく、繰り返しアラームを設定しましたか?あなたが見ている行動を変えるべきではありませんが、誰が知っていますか? – zmbq

関連する問題