我尝试了很多示例,当然包括Android文档中的一个,但没有一个起作用。
这是我的代码:
package wentaas.odaklan
import android.annotation.SuppressLint
import android.app.*
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Layout
import android.text.format.DateFormat
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.View.TEXT_ALIGNMENT_CENTER
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import kotlinx.android.synthetic.main.activity_main.*
import android.content.Context.ALARM_SERVICE
import android.support.v4.content.ContextCompat.getSystemService
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.support.v4.content.ContextCompat.startActivity
import android.support.v4.content.IntentCompat
import android.content.Intent.makeRestartActivityTask
import android.content.ComponentName
import android.content.pm.PackageManager
import android.os.Build
import android.os.PowerManager
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import android.widget.*
import java.util.*
//fun makeAlarms(context: Context) {
// val t = TinyDB(context)
// val hour = t.getListInt("hour")
// val minute = t.getListInt("minute")
//
// Toast.makeText(context.applicationContext, "$hour\n$minute", Toast.LENGTH_SHORT).show()
//
// var i = 0
// for (item in hour) {
// val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
// val alarmIntent = Intent(context, MainActivity::class.java).let { intent ->
// PendingIntent.getBroadcast(context, 0, intent, 0)
// }
//
// alarmMgr.cancel(alarmIntent)
//
// val calendar = Calendar.getInstance().apply {
// timeInMillis = System.currentTimeMillis()
// set(Calendar.HOUR_OF_DAY, hour[i])
// set(Calendar.MINUTE, minute[i])
// }
// alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, AlarmManager.INTERVAL_DAY, alarmIntent)
//
// i += 1
// }
//}
class MainActivity : AppCompatActivity() {
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// setSupportActionBar(toolbar)
//
// val t = TinyDB(this)
// t.clear()
// val is24 = DateFormat.is24HourFormat(this)
// var hour = t.getListInt("hour")
// var minute = t.getListInt("minute")
//
val am = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val date = Date()
val alarmCal = Calendar.getInstance()
val nowCal = Calendar.getInstance()
nowCal.time = date
alarmCal.time = date
alarmCal.set(Calendar.HOUR_OF_DAY, 10)
alarmCal.set(Calendar.MINUTE, 55)
alarmCal.set(Calendar.SECOND, 0)
val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmCal.timeInMillis, 1000*60, pendingIntent)
//
// fun put() {
// t.putListInt("hour", hour)
// t.putListInt("minute", minute)
// }
//
// if (hour.isEmpty() || minute.isEmpty()) {
// hour.clear()
// minute.clear()
// hour.add(10)
// minute.add(30)
// put()
// }
//
// fun toText(hour: Int, minute: Int): String {
// if (is24) {
// val hourText = when (hour) {
// in 0..9 -> "0$hour"
// else -> "$hour"
// }
// val minuteText = when (minute) {
// in 0..9 -> "0$minute"
// else -> "$minute"
// }
// return "$hourText:$minuteText"
// } else {
// val hourText: String
// val ampm: String
// when (hour) {
// 0 -> {
// hourText = "12"
// ampm = "AM"
// }
// in 1..9 -> {
// hourText = "0$hour"
// ampm = "AM"
// }
// in 10..12 -> {
// hourText = "$hour"
// ampm = "AM"
// }
// in 13..23 -> {
// hourText = "${hour - 12}"
// ampm = "PM"
// }
// else -> {
// hourText = "$hour"
// ampm = "AM"
// }
// }
// val minuteText = when (minute) {
// in 0..9 -> "0$minute"
// else -> "$minute"
// }
// return "$hourText:$minuteText $ampm"
// }
// }
//
// fun load() {
//
// hour = t.getListInt("hour")
// minute = t.getListInt("minute")
// val time = mutableListOf<Button>()
//
// for (item in layout.touchables) {
// layout.removeView(item)
// }
//
// var i = 0
// for (item in hour) {
// val button = Button(this)
//
// button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30F)
// val p = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
// p.gravity = Gravity.CENTER
// button.layoutParams = p
// button.gravity = Gravity.CENTER
//
// time.add(button)
// layout.addView(button)
//
// i += 1
// }
//
// makeAlarms(this)
//
//
// text.text = "$hour\n$minute\n$time\n${t.getListInt("hour")}\n${t.getListInt("minute")}"
//
// i = 0
// for (item in time) {
// time[i].text = toText(hour[i], minute[i])
// time[i].setOnClickListener {
// val w = time.indexOf(it)
//
// val timePickerDialog = TimePickerDialog(
// this@MainActivity, R.style.CustomDatePickerDialog,
// TimePickerDialog.OnTimeSetListener {_, sHour, sMinute ->
// hour[w] = sHour
// minute[w] = sMinute
//
// put()
// load()
// }, hour[w], minute[w], is24)
// timePickerDialog.show()
//
// val deleteButton = timePickerDialog.getButton(Dialog.BUTTON_NEUTRAL)
// deleteButton.text = getString(R.string.delete)
// if (w == 0) {
// deleteButton.isEnabled = false
// }
// deleteButton.visibility = VISIBLE
// deleteButton.setOnClickListener {
// hour.removeAt(w)
// minute.removeAt(w)
//
// put()
// timePickerDialog.dismiss()
// load()
// }
// }
// i += 1
// }
// }
//
// fab.setOnClickListener {
// val timePickerDialog = TimePickerDialog(
// this@MainActivity, R.style.CustomDatePickerDialog,
// TimePickerDialog.OnTimeSetListener {_, sHour, sMinute ->
// hour.add(sHour)
// minute.add(sMinute)
//
// put()
// load()
// }, hour.last(), minute.last(), is24)
// timePickerDialog.show()
// }
//
//// load()
// }
//}
//
//class BootReceiver : BroadcastReceiver() {
//
// override fun onReceive(context: Context, intent: Intent) {
// if (intent.action == "android.intent.action.BOOT_COMPLETED") {
// makeAlarms(context)
// }
}
}
//
class AlarmReceiver: BroadcastReceiver() {
override fun onReceive(context:Context, intent:Intent) {
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
val wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "1:")
wl.acquire(10*60*1000L)
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show()
wl.release()
//
// val builder = NotificationCompat.Builder(context, "1")
// .setSmallIcon(R.mipmap.ic_launcher_round)
// .setContentTitle(context.getString(R.string.title))
// .setContentText(context.getString(R.string.text))
//
// NotificationManagerCompat.from(context).notify(1, builder.build())
}
}
如果所有评论都很重要,我将其包括在内。它不会发出任何错误,只是不会触发。我从10:53到10:56尝试了这段代码。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="wentaas.odaklan">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--<receiver android:name=".BootReceiver"-->
<!--android:enabled="true">-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.BOOT_COMPLETED">-->
<!--</action>-->
<!--</intent-filter>-->
<!--</receiver>-->
<receiver android:name=".AlarmReceiver">
</receiver>
</application>
</manifest>
先谢谢了。任何帮助将非常感激。我要疯了!