如何在后台重复闹钟

时间:2018-12-12 12:24:01

标签: android

我每15分钟尝试发出一次警报,问题是我听说当应用未运行时警报会起作用,但在我的情况下,警报会在屏幕上出现时第一次甚至第二次起作用。否则不会。我还想知道何时设置了第一个闹铃,时间是在上一次时间,闹铃会在最近的时间开始,但是如果我将待处理的意图设置为24小时,并且那时手机已关闭,并且在一两个小时,是否有待在25或26个小时后进行日常工作的未决意图。请帮助 这是我的代码。

public class MainActivity extends AppCompatActivity {
    Calendar calendar=Calendar.getInstance();
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.bt);

        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY,13);
        calendar.set(Calendar.MINUTE,54);
        calendar.set(Calendar.SECOND,1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.set(Calendar.HOUR_OF_DAY,13);
                calendar.set(Calendar.MINUTE,54);
                calendar.set(Calendar.SECOND,1);
setAlarm(calendar.getTimeInMillis());
            }
        });
    }
    private void setAlarm(long time) {

        calendar.setTimeInMillis(System.currentTimeMillis());
        //getting the alarm manager
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        //creating a new intent specifying the broadcast receiver
        Intent i = new Intent(this, MyAlarm.class);

        //creating a pending intent using the intent
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        //setting the repeating alarm that will be fired every day
        am.setRepeating(AlarmManager.RTC, time, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
        Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
    }
}

这是我的服务

public class MyAlarm extends BroadcastReceiver{
    MediaPlayer mp;
    @Override
    public void   onReceive(Context context,Intent intent){
mp=MediaPlayer.create(context,R.raw.song);
mp.start();
    }
}

清单在这里

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bilal.pkr">
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>


    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.BOOT_COPLETED"></action>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".MyAlarm"
            android:enabled="true"
            android:exported="true" />
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

am.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);

使用它。