英文:
App is not running in Background (Android Studio java)
问题
以下是你要翻译的内容:
我正在开发一个 Android 应用程序,我想要激活每日闹钟(我只是用 5 分钟间隔作为示例进行测试)。
我使用了一个广播接收器(在清单文件中声明的静态接收器),但是应用程序仍然无法正常工作。以下是我的代码:
清单文件:
<receiver android:name=".ExecutableService" android:enabled="true"></receiver>
AlarmHandler 类:
public class AlarmHandler {
private Context context;
public AlarmHandler(Context context) {
this.context = context;
}
// 这将激活闹钟
public void setAlarmManager() {
Intent intent = new Intent(context, ExecutableService.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 2, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
long triggerAfter = 60 * 5 * 1000; // 这将在 5 分钟后触发服务
long triggerEvery = 60 * 5 * 1000; // 这将在之后的每 5 分钟重复触发闹钟
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAfter, triggerEvery, sender);
}
}
// 这将取消闹钟
public void cancelAlarm() {
Intent intent = new Intent(context, ExecutableService.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 2, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.cancel(sender);
}
}
}
这是广播接收器:
import ...
public class ExecutableService extends BroadcastReceiver {
private static final String TAG = "Executable Service";
@Override
public void onReceive(Context context, Intent intent) {
// 这将在选定的间隔执行,显示通知
Toast.makeText(context, "Hello World 2!", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onReceive: it worked");
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
// 在 API 26 中已弃用
v.vibrate(500);
}
}
}
这是 MainActivity,在这里我激活闹钟:
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
AlarmHandler alarmHandler = new AlarmHandler(this);
// 取消先前安排的闹钟
alarmHandler.cancelAlarm();
// 在一小时后设置新闹钟
alarmHandler.setAlarmManager();
Toast.makeText(this, "Alarm Set!", Toast.LENGTH_SHORT).show();
}
}
如果这不是在后台运行应用程序并在特定时间推送通知(或在特定时间显示简单提示)的正确方法,那么最佳方法是什么?我还尝试过作业调度服务。
英文:
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:
</activity> <receiver android:name=".ExecutableService" android:enabled="true" ></receiver </application>
The AlarmHandler class:
public class AlarmHandler {
private Context context;
public AlarmHandler(Context context) {
this.context = context;
}
//This will active the alarm
public void setAlarmManager(){
Intent intent = new Intent(context,ExecutableService.class);
PendingIntent sender = PendingIntent.getBroadcast(context ,2,intent,0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
long triggerAfter=60*5*1000;//this will trigger the service after 5 min
long triggerEvery=60*5*1000;//this will repeat alarm every 5 min after that
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,triggerAfter,triggerEvery,sender);
}
}
//This will cancel the alarm
public void cancelAlarm (){
Intent intent = new Intent(context,ExecutableService.class);
PendingIntent sender = PendingIntent.getBroadcast(context,2,intent,0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.cancel(sender);
}
}
}
This is the Broadcast receiver:
import ...
public class ExecutableService extends BroadcastReceiver {
private static final String TAG="Executable Service";
@Override
public void onReceive(Context context, Intent intent) {
//this will be executed at selected interval Notification show
Toast.makeText(context, "Hello World 2! ", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onReceive: it worked ");
Vibrator v=(Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(500);
}}}
And this is the MainActivty where I activate the alarm:
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
AlarmHandler alarmHandler = new AlarmHandler(this);
//cancel the previous scheduled alarm
alarmHandler.cancelAlarm();
//set the new alarm after one hour
alarmHandler.setAlarmManager();
Toast.makeText(this, "Alarm Set ! ", Toast.LENGTH_SHORT).show();
}
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论