2017-01-08 13 views
0

私のアプリケーションでは、alarmManagerを使用して何かを実行するようにしています(たとえば24時間ごとなど)。AndroidのAlarmManagerが正しく動作しない場合があります

そのほぼ完璧に働いていますが、ここで見ることができるようにテストの目的のために、私は、5000に私の間隔を定義した:

MainActivity.java:

public void startAlarm() { 
    manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    int interval = 5000; 
      //86400000; 
    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent); 
} 

マイAlarmReceiver.javaを:

public class AlarmReceiver extends BroadcastReceiver { 
QuestsHelper db = new QuestsHelper(MainActivity.appContext); 

@Override 
public void onReceive(Context context, Intent intent) { 

    // For our recurring task, we'll just display a message 
    Random rand = new Random(); 
    long id = rand.nextInt(10); 

    newQuest = db.getRandomQuest(id+1); 

    MainActivity.desc.setText(newQuest.getDesc()); 
    MainActivity.status.setText(String.valueOf(newQuest.getProgress())); 
    MainActivity.exp.setText(String.valueOf(newQuest.getExp())); 

    SharedPreferences DadosNewQuest = PreferenceManager.getDefaultSharedPreferences(context); 
    SharedPreferences.Editor editor = DadosNewQuest.edit(); 
    editor.putString("desc", newQuest.getDesc()); 
    editor.putInt("status", newQuest.getProgress()); 
    editor.putInt("exp", newQuest.getExp()); 
    editor.apply(); 

    SharedPreferences shareprefs = context.getSharedPreferences("cscs", MODE_PRIVATE); 
    SharedPreferences.Editor editor2 = shareprefs.edit(); 
    editor2.putLong("cscs", newQuest.getCsGoal()); 
    editor2.commit(); 

    showNewQuestNotification(); 

    Log.d("AlarmReceiver", "TRIGGER"); 
} 

private void showNewQuestNotification() { 

    NotificationCompat.Builder builder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(appContext) 
        .setSmallIcon(R.drawable.lpq) 
        .setContentTitle("New Quest available") 
        .setContentText("You have a new quest to do !"); 

    // para vibrar: 
    // builder.setVibrate(new long[] { 1000, 1000}); 
    builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 

    Intent notificationIntent = new Intent(appContext, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(appContext, 0, notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(contentIntent); 
    // Add as notification 
    NotificationManager manager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(0, builder.build()); 
} 
} 

下記はexecutiのようなngの間隔が5000

Logcat

コンプリートMainActivity.java

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

public static Context appContext; 
private PendingIntent pendingIntent; 
private AlarmManager manager; 
private Game selectedGame; 
private SessionManager session; 
private static final String TAG = "teste"; 
private static final String TAG2 = "teste2"; 
public static TextView desc; 
public static TextView status; 
public static TextView exp; 
public EditText search; 
public Button button; 
public Long csjogo = null; // minions farmados 
public static Quest newQuest = new Quest(); 

String URL = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_about); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    appContext=getApplicationContext(); 

    final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); 
    final SharedPreferences shareprefs = getSharedPreferences("cscs", MODE_PRIVATE); 
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences DadosNewQuest = PreferenceManager.getDefaultSharedPreferences(this); 

    session = new SessionManager(getApplicationContext()); 

    desc = (TextView) findViewById(R.id.desc); 
    status = (TextView) findViewById(R.id.status); 
    exp = (TextView) findViewById(R.id.exp); 
    search = (EditText) findViewById(R.id.search); 
    button = (Button) findViewById(R.id.button); 

    Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 

    if (!prefs.getBoolean("firstTime", false)) { 
     Log.d(TAG, "onCreate: CORREU"); 
     startAlarm(); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putBoolean("firstTime", true); 
     editor.commit(); 
    } 

    desc.setText(DadosNewQuest.getString("desc", "")); 
    status.setText(String.valueOf(DadosNewQuest.getInt("status", 0))); 
    exp.setText(String.valueOf(DadosNewQuest.getInt("exp", 0))); 

    Log.d("lul", DadosNewQuest.getString("desc", "")); 
    Log.d("lul", String.valueOf(DadosNewQuest.getInt("status", 0))); 
    Log.d("lul", String.valueOf(DadosNewQuest.getInt("exp", 0))); 

    MainActivity.status.setText(String.valueOf(newQuest.getProgress())); 
    MainActivity.exp.setText(String.valueOf(newQuest.getExp())); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(MainActivity.this, "A verificar ...", Toast.LENGTH_SHORT).show(); 
      verifyQUEST(sharedPref.getString("username", ""), shareprefs.getLong("cscs",0)); 
     } 
    }); 

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    AsyncRiotAPI.setRegion(Region.EUW); 
    AsyncRiotAPI.setAPIKey(""); 

    Log.d(TAG2, String.valueOf(shareprefs.getLong("cscs",0))); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

public void startAlarm() { 
    manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    int interval = 5000; 
      //86400000; 
    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent); 
} 

public void cancelAlarm() { 
    if (manager != null) { 
     manager.cancel(pendingIntent); 
     Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show(); 
    } 
} 

public void verifyQUEST(String nick, final long csquest) { 
    AsyncRiotAPI.getRecentGames(new Action<List<Game>>() { 
     @Override 
     public void perform(final List<Game> games) { 

      runOnUiThread(new Runnable() { 
       public void run() { 

        selectedGame = games.get(0); 
        csjogo = (long) selectedGame.getStats().getMinionsKilled() + selectedGame.getStats().getNeutralMinionsKilledEnemyJungle() + selectedGame.getStats().getNeutralMinionsKilledYourJungle(); 
        Log.d(TAG, String.valueOf(csjogo)); 

        if (csjogo >= csquest) { 
         final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
         ViewDialog alert = new ViewDialog(); 
         alert.showDialog(MainActivity.this, "Congratulations, quest completed !", csquest, csjogo); 
         addPoints(newQuest.getExp(), sharedPref.getString("username", "")); 
        } else { 
         NegativeViewDialog alert = new NegativeViewDialog(); 
         alert.showDialog(MainActivity.this, "What a noob, you failed the quest !", csquest, csjogo); 
        } 
       } 
      }); 
     } 
     @Override 
     public void handle(APIException e) { 
      Log.d(TAG, "ERRO, RIP"); 
     } 
    }, nick); 
} 

private void addPoints (final int points, final String nickname) { 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        System.out.println(response); 
        Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show(); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show(); 
       } 
      }){ 
     @Override 
     protected Map<String,String> getParams(){ 
      Map<String,String> params = new HashMap<String, String>(); 
      params.put("points", String.valueOf(points)); 
      params.put("nickname", nickname); 
      return params; 
     } 

    }; 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.about, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_login) { 
     if (session.isLoggedIn()) { 
      Toast.makeText(appContext, "You are already logged in !", Toast.LENGTH_SHORT).show(); 
     } else { 
      Intent intent = new Intent(MainActivity.this, LoginActivity.class); 
      startActivity(intent); 
     } 
    } else if (id == R.id.nav_about) { 

    } else if (id == R.id.nav_profile) { 
     Intent intent = new Intent(MainActivity.this, ProfileQuest.class); 
     startActivity(intent); 
    } else if (id == R.id.nav_manage) { 
     Intent intent = new Intent(MainActivity.this, QuestsActivity.class); 
     startActivity(intent); 
    } else if (id == R.id.nav_board) { 

    } else if (id == R.id.nav_contacts) { 

    } else if (id == R.id.nav_logout) { 
     if (session.isLoggedIn()) { 
      session.setLogin(false); 
     } 
     Toast.makeText(appContext, "logout", Toast.LENGTH_SHORT).show(); 
     Intent intent = new Intent(MainActivity.this, LoginActivity.class); 
     startActivity(intent); 
    } else if (id == R.id.nav_settings) { 
     Intent intent = new Intent(MainActivity.this, UserSettings.class); 
     startActivity(intent); 
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
} 

どれtoughtsに設定されているにもかかわらず、一人一人分?

答えて

1

アラームマネージャでは、どのモードでも20秒に1回以上の発射を許可していません。より頻繁に必要な場合は、アラームではなくハンドラとpostDelayedを使用してください。

setRepeatingを使用して、Androidは、システムがタイマーの起動によるバッテリ電力を最小限に抑えるために、より便利な時間にあなたの予定を変更する権利を留保します。 setExactは、より速いタイマーを得るために必要なメソッドですが、絶対に必要な場合にのみ使用してください。

新しいAndroidでは、DozeはsetExactによって設定されたタイマーをすべて停止します。ただし、Dozeがオンになると15分ごとに小さなウィンドウが表示されます。 setExactAndAllowWhileIdleはDoze中に起動しますが、目覚まし時計や会議リマインダなどの非常に重要なタイマーにのみ使用してください。

関連する問題