安卓客户端 android开源项目源码,完整任务倒计时提醒源码,下载文件作为模块引入即可。
提示:创建好任务后,点击任务名称 才能真正开始倒计时任务,到点时开始播放音乐提醒,请测试
package com.example.myalarm;import android.app.AlarmManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.PowerManager;import android.os.SystemClock;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.Toast;import java.util.Map;public class MainActivity extends AppCompatActivity { private SharedPreferences sps ; int alarmKey, alarmTime, workNum; private LinearLayout ll_work; private int alarmId = 1; PowerManager powerManager = null; PowerManager.WakeLock wakeLock = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// 设置屏幕常亮 powerManager = (PowerManager)this.getSystemService(this.POWER_SERVICE); wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock"); ll_work = (LinearLayout) findViewById(R.id.ll_work); ll_work.removeAllViews(); sps = getSharedPreferences("share", Context.MODE_PRIVATE); workNum = sps.getInt("workNum",0); for(int i = 0 ; i < workNum ; i ){ final LinearLayout ll= (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.add_button,null); ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); final Button btn=(Button)ll.findViewById(R.id.work_btn); String textKey = "work" (i 1); String btnText = sps.getString(textKey,""); btn.setText(btnText); ll_work.addView(ll); } for(int j=0;j<ll_work.getChildCount();j ){ final Button bt=(Button) ll_work.getChildAt(j).findViewById(R.id.work_btn); final int finalJ = j; bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// Button btnEvent = (Button) findViewById(v.getId());// String btnMsg = (String) btnEvent.getText(); alarmId = alarmId 1; String btnMsg = (String) bt.getText(); alarmKey = v.getId();// 闹钟倒计时时间 alarmTime = sps.getInt(btnMsg,0); alarmTime=alarmTime*1000;//换成毫秒 Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); intent.putExtra("task",btnMsg); intent.putExtra("alarmKey",alarmKey); PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this,alarmKey alarmId,intent,0); long firstTime = SystemClock.elapsedRealtime(); // 开机之后到现在的运行时间(包括睡眠时间) firstTime = alarmTime; // 进行闹铃注册 AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE); //manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 3000, sender);//重复闹钟 manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, pi); Toast.makeText(MainActivity.this,btnMsg "任务倒计时开始",Toast.LENGTH_SHORT).show(); } }); } }// public void setAlarm(View v){// Button btnEvent = (Button) findViewById(v.getId());// String btnMsg = (String) btnEvent.getText();//// alarmKey = v.getId();//// 闹钟倒计时时间// alarmTime = sps.getInt(btnMsg,0);// alarmTime=alarmTime*1000;//换成毫秒//// Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);// intent.putExtra("task",btnMsg);// intent.putExtra("alarmKey",alarmKey);//// PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this,alarmKey,intent,0);//// long firstTime = SystemClock.elapsedRealtime(); // 开机之后到现在的运行时间(包括睡眠时间)// firstTime = alarmTime;//// // 进行闹铃注册// AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);// //manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 3000, sender);//重复闹钟// manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, pi);//// Toast.makeText(MainActivity.this,btnMsg "任务倒计时开始",Toast.LENGTH_SHORT).show();// } @Override protected void onResume() { super.onResume(); wakeLock.acquire(); } @Override protected void onPause() { super.onPause(); wakeLock.release(); }}
评论