返回顶部

收藏

Android 一个简单的警告服务,使用 AlarmManager

更多

适合 Android 初学者

AndroidAlarmService_01.png

imgs/asCode/19095648_kMXm.png

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startalarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
/>
<Button
android:id="@+id/cancelalarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
/>
</LinearLayout>

AndroidAlarmService.java

package com.exercise.AndroidAlarmService;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidAlarmService extends Activity {

private PendingIntent pendingIntent;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button buttonStart = (Button)findViewById(R.id.startalarm);
    Button buttonCancel = (Button)findViewById(R.id.cancelalarm);

    buttonStart.setOnClickListener(new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub

  Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
  pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);

           AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

           Calendar calendar = Calendar.getInstance();
           calendar.setTimeInMillis(System.currentTimeMillis());
           calendar.add(Calendar.SECOND, 10);
           alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

  Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
 }});

    buttonCancel.setOnClickListener(new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
  alarmManager.cancel(pendingIntent);

           // Tell the user about what we did.
           Toast.makeText(AndroidAlarmService.this, "Cancel!", Toast.LENGTH_LONG).show();

 }});

}
}

MyAlarmService.java

package com.exercise.AndroidAlarmService;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyAlarmService extends Service {

@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.exercise.AndroidAlarmService"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".AndroidAlarmService"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<service android:name=".MyAlarmService" />
</application>
<uses-sdk android:minSdkVersion="4" />

</manifest>

标签:Android,SDK

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2017-08-12 07:58:00 android - android sdk 和 studio 的奇怪问题: 重装. 和升级高更高版
  2. 奋斗小土豆丶 发表 2015-11-04 08:07:43 Eclispe环境启动遇This&nbsp;Android&nbsp;SDK&nbsp;requir
  3. oasisfeng 发表 2014-03-16 08:20:17 对Android Wearable SDK的猜想
  4. 博主 发表 2017-12-27 08:28:37 Android埋点技术分析
  5. K-Res 发表 2016-01-31 07:36:02 iOS Android平台下的wchar_t默认size问题
  6. 博主 发表 2014-08-29 15:25:00 android-sdk
  7. admin 发表 2018-02-25 03:46:02 Android Wear SDK 和模拟器更新
  8. Javen Fang 发表 2014-02-18 01:00:21 RegistrationID 精确对点推送
  9. admin 发表 2018-02-25 03:46:02 Android Wear SDK 和模拟器更新
  10. ANIL K. 发表 2018-05-03 09:31:10 Android 4.0 SDK: Crash during stream subscription
  11. Aj 发表 2016-01-05 04:51:29 几种Android模拟器对比
  12. 博主 发表 2014-12-22 01:07:22 让AndroidSDK飞

发表评论