首頁 收藏 QQ群
 網(wǎng)站導航

ZNDS智能電視網(wǎng) 推薦當貝市場

TV應用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網(wǎng)站活動 | Z幣中心

新手入門 / 進階 / 社區(qū)互助

新手 | 你問我答 | 免費刷機救磚 | ROM固件

查看: 15502|回復: 0
上一主題 下一主題
[源碼]

android鬧鐘——原代碼

[復制鏈接]
跳轉到指定樓層
樓主
發(fā)表于 2013-8-28 16:27 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
8手機當中肯定是有鬧鐘的,這是不用說的,要不然就不是手機了。對吧。   
下面我根據(jù)廣播的方式寫了一個鬧鐘,大家請看圖:   
   
下面讓我們看一下原代碼的是如何寫的,   
package com.smart;   
   
import java.util.Calendar;   
   
import android.app.Activity;   
import android.content.BroadcastReceiver;   
import android.content.Context;   
import android.content.Intent;   
import android.content.SharedPreferences;   
import android.media.MediaPlayer;   
   
public class AlarmReceiver extends BroadcastReceiver {   
/**   
* 通過廣播進行掃描,是否到達時間后再響起鬧鈴   
*   
* */   
    @Override   
    public void onReceive(Context context, Intent intent) {   
   
           
        SharedPreferences sharedPreferences=context.getSharedPreferences("alarm_record", Activity.MODE_PRIVATE);   
        String hour=String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));   
        String minute=String.valueOf(Calendar.getInstance().get(Calendar.MINUTE));   
        String time=sharedPreferences.getString(hour+":"+minute, null);//小時與分,   
           
        if(time!=null){//判斷是否為空,然后通過創(chuàng)建,   
            MediaPlayer mediaPlayer=MediaPlayer.create(context, R.raw.llb);   
            mediaPlayer.start();//開始   
        }   
      
    }   
   
}   
package com.smart;   
   
import android.app.Activity;   
import android.app.AlarmManager;   
import android.app.AlertDialog;   
import android.app.PendingIntent;   
import android.content.Context;   
import android.content.DialogInterface;   
import android.content.Intent;   
import android.content.SharedPreferences;   
import android.os.Bundle;   
import android.view.View;   
import android.view.View.OnClickListener;   
import android.widget.Button;   
import android.widget.TextView;   
import android.widget.TimePicker;   
   
public class Main extends Activity implements OnClickListener{   
      
    private TextView alarmRecord;   
    private SharedPreferences sharedPreferences;   
      
      
    @Override   
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
        Button addAlarm=(Button)findViewById(R.id.addAlarm);   
        alarmRecord=(TextView)findViewById(R.id.alarmRecord);   
        addAlarm.setOnClickListener(this);   
        sharedPreferences=getSharedPreferences("alarm_record", Activity.MODE_PRIVATE);   
        AlarmManager aManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);   
        Intent intent=new Intent(this,AlarmReceiver.class);   
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, intent, 0);   
        aManager.setRepeating(AlarmManager.RTC, 0, 60*1000, pendingIntent);   
      
    }   
   
      
    @Override   
    public void onClick(View v) {   
        View view=getLayoutInflater().inflate(R.layout.llb,    null);//   
        final TimePicker timePicker=(TimePicker)view.findViewById(R.id.timepicker);   
        timePicker.setIs24HourView(true);//   
        new AlertDialog.Builder(this).setTitle("設置鬧鈴時間").setView(view).setPositiveButton("確定", new DialogInterface.OnClickListener() {   
            //設置標題   
            @Override   
            public void onClick(DialogInterface dialog, int which) {   
            //按鈕事件觸發(fā)方法   
                String timeStr=String.valueOf(timePicker.getCurrentHour())+":"+String.valueOf(timePicker.getCurrentMinute());   
                alarmRecord.setText(alarmRecord.getText().toString()+"
"+timeStr);   
                sharedPreferences.edit().putString(timeStr, timeStr).commit();   
                  
            }   
        }).setNegativeButton("取消", null).show();   
    }   
}原代碼下載   
   

上一篇:Android騰訊微博客戶端開發(fā)二:相關工具篇
下一篇:第十九講:Android Notification的使用入門
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

Archiver|新帖|標簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號 )

網(wǎng)絡信息服務信用承諾書 | 增值電信業(yè)務經營許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號

GMT+8, 2024-10-20 03:18 , Processed in 0.057030 second(s), 15 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復 返回頂部 返回列表