简体   繁体   English

应用程序不在后台运行(Android Studio java)

[英]App is not running in Background (Android Studio java)

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).我正在开发一个 Android 应用程序,我想激活每日警报(我使用 5 分钟间隔作为测试示例)。

I used a Brodacast receiver (Static one declared in the manifest file), but the app still doesn't work.我使用了 Brodacast 接收器(在清单文件中声明的静态接收器),但该应用程序仍然无法运行。 Here's my code:这是我的代码:

The Manifest file:清单文件:

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

The AlarmHandler class: AlarmHandler 类:

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:这是我激活警报的 MainActivty:

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.我也试过jobscheduler服务。

you set the alarm start time to long triggerAfter=60*5*1000;您将闹钟开始时间设置为long triggerAfter=60*5*1000; I suggest changing this to long triggerAfter =60*5*1000+ System.currentTimeMillis()我建议将其更改为long triggerAfter =60*5*1000+ System.currentTimeMillis()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM