2016-09-01 10 views
1

サービスを作成してアラームを設定しようとしています。私は、バックグラウンドでサービスを実行する必要があります。 START_STICKYを使用してみました。サービスは独立した子プロセスとグローバルプロセスで実行されていましたが、すべて無駄でした。私は何をすべきか ?AndroidはSTART_STICKYを使用していても再作成していません

Alarm.java

package com.simpleapps.simpleweather; 
import ... 

public class Alarm extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100); 
     toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,150); 
     Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); 
    } 

    public void setAlarm(Context context) 
    { 
     Log.d("Alarm","Started"); 
     AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, Alarm.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60, pi); // Millisec * Second * Minute 
    } 

    public void cancelAlarm(Context context) 
    { 
     Intent intent = new Intent(context, Alarm.class); 
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.cancel(sender); 
    } 
} 

HelloService.java

package com.simpleapps.simpleweather; 
import ... 

public class HelloService extends Service 
{ 
    Alarm alarm = new Alarm(); 
    public void onCreate() 
    { 
     super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
     Log.d("Alarm","New Service"); 
     alarm.setAlarm(this); 
     return START_STICKY; 
    } 

    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     alarm.setAlarm(this); 
    } 

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

たManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.simpleapps.simpleweather"> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".today_details" /> 
     <activity android:name=".forecast_details"/> 
     <receiver android:process=":remote" android:name=".Alarm" android:exported="true"> 
     </receiver> 

     <service 
      android:name=".HelloService" 
      android:enabled="true"> 
     </service> 

    </application> 

</manifest> 

サービスの開始 -

Intent intent = new Intent(this, HelloService.class); 
startService(intent); 

更新:それは私はあなたのコードを試してみた、それはエミュレータ上で[OK]を働いている私のロリポップ5.0デバイスAsusZenfone2

答えて

0

を除くすべてのデバイスに取り組んでいます。

public class Alarm extends BroadcastReceiver 
{ 
    private static boolean sAlarmCanceled = false; 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     if (sAlarmCanceled) 
      return; 


     ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100); 
     toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,150); 
     Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_SHORT).show(); 
     scheduleNextAlarm(context); 
    } 

    public static void enableAlarm(Context context) { 
     sAlarmCanceled = false; 
     scheduleNextAlarm(context); 
    } 

    public static void disableAlarm() { 
     sAlarmCanceled = true; 
    } 

    private static void scheduleNextAlarm(Context context) 
    { 
     AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, Alarm.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_ONE_SHOT); 
     am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pi); 
    } 
} 

そして今:

* setRepeating 
* <b>Note:</b> Beginning in API 19, the trigger time passed to this method 
* is treated as inexact: the alarm will not be delivered before this time, but 
* may be deferred and delivered some time later. The OS will use 
* this policy in order to "batch" alarms together across the entire system, 
* minimizing the number of times the device needs to "wake up" and minimizing 
* battery use. In general, alarms scheduled in the near future will not 
* be deferred as long as alarms scheduled far in the future. 

だから私は、そう見えるようにアラームを(それはちょうどイラストだ、あなたは静的フィールドなしでそれを行うことができます)に変更しました:私は予告をしたことの一つは、ということです10秒ごとに正確に実行されます。サービスは正常に実行され、アプリケーションタスクを削除して、とにかくアラームを確認することができました。

+0

エミュレータで作業していますが、デバイスで動作していませんLollipop 5.0 [AsusZenfone2](http://www.gsmarena.com/asus_zenfone_2_ze551ml-6917.php)。何が間違っているのでしょうか? –

関連する問題