zoukankan      html  css  js  c++  java
  • 设置和取消闹钟

    <?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:id="@+id/TextView"
            android:layout_height="wrap_content" />
        <Button android:text="设置闹钟" android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </Button>
        <Button android:text="取消闹钟" android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </Button>
    </LinearLayout>
    View Code

    MainActivity2.java   设置闹钟和取消闹钟

    package com.example.textdemo5;
    
    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.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity2 extends Activity {
        private TextView tv = null;
        private Button btn_set = null;
        private Button btn_cel = null;
        private Calendar c = null;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) this.findViewById(R.id.TextView);
            btn_set = (Button) this.findViewById(R.id.Button01);
            btn_cel = (Button) this.findViewById(R.id.Button02);
            c = Calendar.getInstance();
            
            c.setTimeInMillis(System.currentTimeMillis());
            int hour = 18;    // 设置的小时
            int minute = 24;  // 设置的分钟
            c.setTimeInMillis(System.currentTimeMillis());
            c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            Intent intent = new Intent(MainActivity2.this,
                    AlamrReceiver.class);
            PendingIntent pi = PendingIntent.getBroadcast(
                    MainActivity2.this, 0, intent, 0);
            AlarmManager am = (AlarmManager) getSystemService(
                    Activity.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP,
                    c.getTimeInMillis(), pi);// 设置闹钟
            am.setRepeating(AlarmManager.RTC_WAKEUP,
                    c.getTimeInMillis(), (10 * 1000), pi);// 重复设置
            tv.setText("设置的闹钟时间为:" + hour + ":"
                    + minute);
            //---------------------------------
            /*btn_set.setOnClickListener(new Button.OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    c.setTimeInMillis(System.currentTimeMillis());
                    
                    new TimePickerDialog(MainActivity2.this,
                            new TimePickerDialog.OnTimeSetListener() {
    
                                public void onTimeSet(TimePicker view,
                                        int hourOfDay, int minute) {
                                    // TODO Auto-generated method stub
                                    c.setTimeInMillis(System.currentTimeMillis());
                                    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                    c.set(Calendar.MINUTE, minute);
                                    c.set(Calendar.SECOND, 0);
                                    c.set(Calendar.MILLISECOND, 0);
                                    Intent intent = new Intent(MainActivity2.this,
                                            AlamrReceiver.class);
                                    PendingIntent pi = PendingIntent.getBroadcast(
                                            MainActivity2.this, 0, intent, 0);
                                    AlarmManager am = (AlarmManager) getSystemService(
                                            Activity.ALARM_SERVICE);
                                    am.set(AlarmManager.RTC_WAKEUP,
                                            c.getTimeInMillis(), pi);// 设置闹钟
                                    am.setRepeating(AlarmManager.RTC_WAKEUP,
                                            c.getTimeInMillis(), (10 * 1000), pi);// 重复设置
                                    tv.setText("设置的闹钟时间为:" + hourOfDay + ":"
                                            + minute);
                                }
                            }, hour, minute, true).show();
                }
            });*/
            
            btn_cel.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(MainActivity2.this,
                            AlamrReceiver.class);
                    PendingIntent pi = PendingIntent.getBroadcast(
                            MainActivity2.this, 0, intent, 0);
                    AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
                    am.cancel(pi);
                    tv.setText("闹钟取消");
                }
            });
        }
    }
    View Code

    AlamrReceiver.java  广播及时弹出闹钟提示框

    package com.example.textdemo5;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class AlamrReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent arg1) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "闹钟时间到", Toast.LENGTH_LONG).show();
        }
    
    }
    
     
    View Code

    AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.textdemo5"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity2"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            
            <receiver android:name=".AlamrReceiver" android:process=":remote" />
            
        </application>
    
    </manifest>
    View Code

    DEMO完整下载路径:http://download.csdn.net/detail/androidsj/5868481

  • 相关阅读:
    JSON基础知识
    Java 环境配置
    接口测试基础知识
    Fiddler初学笔记
    es6数组方法findIndex()
    sass+less相关
    前端库/框架/插件相关
    知名博主相关
    CSS相关
    移动Web相关
  • 原文地址:https://www.cnblogs.com/androidsj/p/3236681.html
Copyright © 2011-2022 走看看