应用程序未在后台运行(Android Studio Java)

huangapple go评论119阅读模式
英文:

App is not running in Background (Android Studio java)

问题

以下是你要翻译的内容:

我正在开发一个 Android 应用程序,我想要激活每日闹钟(我只是用 5 分钟间隔作为示例进行测试)。

我使用了一个广播接收器(在清单文件中声明的静态接收器),但是应用程序仍然无法正常工作。以下是我的代码:

清单文件:

  1. <receiver android:name=".ExecutableService" android:enabled="true"></receiver>

AlarmHandler 类:

  1. public class AlarmHandler {
  2. private Context context;
  3. public AlarmHandler(Context context) {
  4. this.context = context;
  5. }
  6. // 这将激活闹钟
  7. public void setAlarmManager() {
  8. Intent intent = new Intent(context, ExecutableService.class);
  9. PendingIntent sender = PendingIntent.getBroadcast(context, 2, intent, 0);
  10. AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  11. if (alarmManager != null) {
  12. long triggerAfter = 60 * 5 * 1000; // 这将在 5 分钟后触发服务
  13. long triggerEvery = 60 * 5 * 1000; // 这将在之后的每 5 分钟重复触发闹钟
  14. alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAfter, triggerEvery, sender);
  15. }
  16. }
  17. // 这将取消闹钟
  18. public void cancelAlarm() {
  19. Intent intent = new Intent(context, ExecutableService.class);
  20. PendingIntent sender = PendingIntent.getBroadcast(context, 2, intent, 0);
  21. AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  22. if (alarmManager != null) {
  23. alarmManager.cancel(sender);
  24. }
  25. }
  26. }

这是广播接收器:

  1. import ...
  2. public class ExecutableService extends BroadcastReceiver {
  3. private static final String TAG = "Executable Service";
  4. @Override
  5. public void onReceive(Context context, Intent intent) {
  6. // 这将在选定的间隔执行,显示通知
  7. Toast.makeText(context, "Hello World 2!", Toast.LENGTH_SHORT).show();
  8. Log.d(TAG, "onReceive: it worked");
  9. Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  10. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  11. v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
  12. } else {
  13. // 在 API 26 中已弃用
  14. v.vibrate(500);
  15. }
  16. }
  17. }

这是 MainActivity,在这里我激活闹钟:

  1. public class MainActivity2 extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main2);
  6. AlarmHandler alarmHandler = new AlarmHandler(this);
  7. // 取消先前安排的闹钟
  8. alarmHandler.cancelAlarm();
  9. // 在一小时后设置新闹钟
  10. alarmHandler.setAlarmManager();
  11. Toast.makeText(this, "Alarm Set!", Toast.LENGTH_SHORT).show();
  12. }
  13. }

如果这不是在后台运行应用程序并在特定时间推送通知(或在特定时间显示简单提示)的正确方法,那么最佳方法是什么?我还尝试过作业调度服务。

英文:

I am working on an Android app and I want to activate a daily Alarm (I used 5 min interval just as an example to test).

I used a Brodacast receiver (Static one declared in the manifest file),
but the app still doesn't work. Here's my code:

The Manifest file:

  1. &lt;/activity&gt; &lt;receiver android:name=&quot;.ExecutableService&quot; android:enabled=&quot;true&quot; &gt;&lt;/receiver &lt;/application&gt;

The AlarmHandler class:

  1. public class AlarmHandler {
  2. private Context context;
  3. public AlarmHandler(Context context) {
  4. this.context = context;
  5. }
  6. //This will active the alarm
  7. public void setAlarmManager(){
  8. Intent intent = new Intent(context,ExecutableService.class);
  9. PendingIntent sender = PendingIntent.getBroadcast(context ,2,intent,0);
  10. AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  11. if (alarmManager != null) {
  12. long triggerAfter=60*5*1000;//this will trigger the service after 5 min
  13. long triggerEvery=60*5*1000;//this will repeat alarm every 5 min after that
  14. alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,triggerAfter,triggerEvery,sender);
  15. }
  16. }
  17. //This will cancel the alarm
  18. public void cancelAlarm (){
  19. Intent intent = new Intent(context,ExecutableService.class);
  20. PendingIntent sender = PendingIntent.getBroadcast(context,2,intent,0);
  21. AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  22. if (alarmManager != null) {
  23. alarmManager.cancel(sender);
  24. }
  25. }
  26. }

This is the Broadcast receiver:

  1. import ...
  2. public class ExecutableService extends BroadcastReceiver {
  3. private static final String TAG=&quot;Executable Service&quot;;
  4. @Override
  5. public void onReceive(Context context, Intent intent) {
  6. //this will be executed at selected interval Notification show
  7. Toast.makeText(context, &quot;Hello World 2! &quot;, Toast.LENGTH_SHORT).show();
  8. Log.d(TAG, &quot;onReceive: it worked &quot;);
  9. Vibrator v=(Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  10. if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
  11. v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
  12. } else {
  13. //deprecated in API 26
  14. v.vibrate(500);
  15. }}}

And this is the MainActivty where I activate the alarm:

  1. public class MainActivity2 extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main2);
  6. AlarmHandler alarmHandler = new AlarmHandler(this);
  7. //cancel the previous scheduled alarm
  8. alarmHandler.cancelAlarm();
  9. //set the new alarm after one hour
  10. alarmHandler.setAlarmManager();
  11. Toast.makeText(this, &quot;Alarm Set ! &quot;, Toast.LENGTH_SHORT).show();
  12. }

If this is not the way that I should use to run the app in the background and push a notification (or a simple toast at a specific time), what is the best way to do it?

I tried also jobscheduler services.

答案1

得分: 1

你将闹钟的开始时间设置为long triggerAfter=60*5*1000;我建议将其更改为long triggerAfter =60*5*1000+ System.currentTimeMillis()

英文:

you set the alarm start time to long triggerAfter=60*5*1000;
I suggest changing this to
long triggerAfter =60*5*1000+ System.currentTimeMillis()

huangapple
  • 本文由 发表于 2020年8月25日 21:31:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63580008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定