如何在 Kotlin Android 中使用 Alarmmanager 启动服务?
此示例演示了如何在 Kotlin Android 中使用 Alarmmanager 启动服务。
步骤 1 − 在 Android Studio 中创建新项目,转至文件 ⇉ 新建项目,并填写所有必需信息以创建新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="16sp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_blue_light" android:textSize="32sp" android:textStyle="bold" /> <Button android:id="@+id/btnStartService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="Start Service Alarm" /> <Button android:id="@+id/btnStopService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Cancel Service" /> </LinearLayout>
步骤 3 − 将以下代码添加到 src/MainActivity.kt
import android.app.AlarmManager import android.app.PendingIntent import android.content.Context import android.content.Intent import android.os.Bundle import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { private lateinit var btnStart: Button private lateinit var btnStop: Button lateinit var pendingIntent: PendingIntent override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" btnStart = findViewById(R.id.btnStartService) btnStop = findViewById(R.id.btnStopService) btnStart.setOnClickListener { val myIntent = Intent(this@MainActivity, MyAlarmService::class.java) pendingIntent = PendingIntent.getService(this@MainActivity, 0, myIntent, 0) val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val calendar: Calendar = Calendar.getInstance() calendar.timeInMillis = System.currentTimeMillis() calendar.add(Calendar.SECOND, 3) alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent) Toast.makeText(baseContext, "Starting Service Alarm", Toast.LENGTH_LONG).show() } btnStop.setOnClickListener { val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.cancel(pendingIntent) Toast.makeText(baseContext, "Service Cancelled", Toast.LENGTH_LONG).show() } } }
步骤 4 − 将以下代码添加到 src/MyAlarmService.kt
import android.app.Service import android.content.Intent import android.os.IBinder import android.widget.Toast @Suppress("DEPRECATION") class MyAlarmService : Service() { override fun onCreate() { Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); super.onCreate() } override fun onBind(intent: Intent?): IBinder? { Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show(); return null } override fun onDestroy() { super.onDestroy() Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show() } override fun onStart(intent: Intent?, startId: Int) { super.onStart(intent, startId) Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show() } override fun onUnbind(intent: Intent?): Boolean { Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show() return super.onUnbind(intent) } }
步骤 5 − 将以下代码添加到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.kotlipapp"> <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.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
尝试运行应用程序。我假设您已将实际的 Android 移动设备与计算机连接。要从 Android Studio 运行应用,请打开项目的一个活动文件,然后单击工具栏中的运行 图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 −
单击此处下载项目代码。
广告