查询网络资源,进行java源文件编写。
1 package com.cn.daming; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 8 /** 9 * Created by 阳光笑容 on 2018/3/3. 10 */ 11 12 public class AlarmAlert extends Activity { 13 protected void onCreate(Bundle savedInstanceState) 14 { 15 super.onCreate(savedInstanceState); 16 new AlertDialog.Builder(AlarmAlert.this) 17 .setIcon(R.drawable.clock) 18 .setTitle("闹钟响了!!") 19 .setMessage("快完成你制定的计划吧!!!") 20 .setPositiveButton("关闭", 21 new DialogInterface.OnClickListener() 22 { 23 public void onClick(DialogInterface dialog, int whichButton) 24 { 25 AlarmAlert.this.finish(); 26 } 27 }) 28 .show(); 29 } 30 }
1 package com.cn.daming; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.os.Bundle; 7 8 /** 9 * Created by 阳光笑容 on 2018/3/3. 10 */ 11 12 public class CallAlerm extends BroadcastReceiver 13 { 14 @Override 15 public void onReceive(Context context, Intent intent) { 16 Intent i=new Intent(context,CallAlerm.class); 17 Bundle bundleRet=new Bundle(); 18 bundleRet.putString("STR_CALLER", ""); 19 i.putExtras(bundleRet); 20 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 21 context.startActivity(i); 22 } 23 }
1 package com.cn.daming; 2 3 import android.app.Activity; 4 import android.app.AlarmManager; 5 import android.app.PendingIntent; 6 import android.app.TimePickerDialog; 7 import android.content.DialogInterface; 8 import android.content.Intent; 9 import android.content.SharedPreferences; 10 import android.support.v7.app.AlertDialog; 11 import android.support.v7.app.AppCompatActivity; 12 import android.os.Bundle; 13 import android.view.KeyEvent; 14 import android.view.LayoutInflater; 15 import android.view.View; 16 import android.widget.Button; 17 import android.widget.EditText; 18 import android.widget.TextView; 19 import android.widget.TimePicker; 20 import android.widget.Toast; 21 import android.widget.Toolbar; 22 23 import java.util.Calendar; 24 25 public class MainActivity extends AppCompatActivity { 26 TextView setTime1; 27 TextView setTime2; 28 TextView setTime3; 29 Button mButton1; 30 Button mButton2; 31 Button mButton3; 32 Button mButton4; 33 Button mButton5; 34 Button mButton6; 35 String time1String =null; 36 String time2String = null; 37 String time3String =null; 38 String defalutString="目前无设置"; 39 AlertDialog builder=null; 40 Calendar c=Calendar.getInstance(); 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.activity_main); 45 android.support.v7.widget.Toolbar toolbar=findViewById(R.id.toolbar); 46 setSupportActionBar(toolbar); 47 //取得活动的Preferences对象 48 SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE); 49 time1String = settings.getString("TIME1", defalutString); 50 time2String = settings.getString("TIME2", defalutString); 51 time3String = settings.getString("TIME3", defalutString); 52 53 InitButton1(); 54 InitButton2(); 55 InitButton3(); 56 InitButton4(); 57 InitButton5(); 58 InitButton6(); 59 60 setTime1.setText(time1String); 61 setTime3.setText(time2String); 62 setTime2.setText(time3String); 63 } 64 65 public void InitButton1() 66 { 67 setTime1=(TextView) findViewById(R.id.setTime1); 68 mButton1=(Button)findViewById(R.id.mButton1); 69 mButton1.setOnClickListener(new View.OnClickListener() 70 { 71 public void onClick(View v) 72 { 73 c.setTimeInMillis(System.currentTimeMillis()); 74 int mHour=c.get(Calendar.HOUR_OF_DAY); 75 int mMinute=c.get(Calendar.MINUTE); 76 77 78 new TimePickerDialog(MainActivity.this, 79 new TimePickerDialog.OnTimeSetListener() 80 { 81 public void onTimeSet(TimePicker view, int hourOfDay, 82 int minute) 83 { 84 c.setTimeInMillis(System.currentTimeMillis()); 85 c.set(Calendar.HOUR_OF_DAY,hourOfDay); 86 c.set(Calendar.MINUTE,minute); 87 c.set(Calendar.SECOND,0); 88 c.set(Calendar.MILLISECOND,0); 89 90 Intent intent = new Intent(MainActivity.this, CallAlerm.class); 91 PendingIntent sender=PendingIntent.getBroadcast( 92 MainActivity.this,0, intent, 0); 93 AlarmManager am; 94 am = (AlarmManager)getSystemService(ALARM_SERVICE); 95 am.set(AlarmManager.RTC_WAKEUP, 96 c.getTimeInMillis(), 97 sender 98 ); 99 String tmpS=format(hourOfDay)+":"+format(minute); 100 setTime1.setText(tmpS); 101 102 //SharedPreferences保存数据,并提交 103 SharedPreferences time1Share = getPreferences(0); 104 SharedPreferences.Editor editor = time1Share.edit(); 105 editor.putString("TIME1", tmpS); 106 editor.commit(); 107 108 Toast.makeText(MainActivity.this,"设置闹钟时间为"+tmpS, 109 Toast.LENGTH_SHORT) 110 .show(); 111 } 112 },mHour,mMinute,true).show(); 113 } 114 }); 115 } 116 117 public void InitButton2() 118 { 119 mButton2=(Button) findViewById(R.id.mButton2); 120 mButton2.setOnClickListener(new View.OnClickListener() 121 { 122 public void onClick(View v) 123 { 124 Intent intent = new Intent(MainActivity.this, CallAlerm.class); 125 PendingIntent sender=PendingIntent.getBroadcast( 126 MainActivity.this,0, intent, 0); 127 AlarmManager am; 128 am =(AlarmManager)getSystemService(ALARM_SERVICE); 129 am.cancel(sender); 130 Toast.makeText(MainActivity.this,"大明闹钟时间删除", 131 Toast.LENGTH_SHORT).show(); 132 setTime1.setText("目前无设置"); 133 134 SharedPreferences time1Share = getPreferences(0); 135 SharedPreferences.Editor editor = time1Share.edit(); 136 editor.putString("TIME1", "目前无设置"); 137 editor.commit(); 138 } 139 }); 140 } 141 142 public void InitButton3() 143 { 144 setTime3=(TextView) findViewById(R.id.setTime5); 145 mButton3=(Button)findViewById(R.id.mButton5); 146 mButton3.setOnClickListener(new View.OnClickListener() 147 { 148 public void onClick(View v) 149 { 150 c.setTimeInMillis(System.currentTimeMillis()); 151 int mHour=c.get(Calendar.HOUR_OF_DAY); 152 int mMinute=c.get(Calendar.MINUTE); 153 154 155 new TimePickerDialog(MainActivity.this, 156 new TimePickerDialog.OnTimeSetListener() 157 { 158 public void onTimeSet(TimePicker view,int hourOfDay, 159 int minute) 160 { 161 c.setTimeInMillis(System.currentTimeMillis()); 162 c.set(Calendar.HOUR_OF_DAY,hourOfDay); 163 c.set(Calendar.MINUTE,minute); 164 c.set(Calendar.SECOND,0); 165 c.set(Calendar.MILLISECOND,0); 166 167 Intent intent = new Intent(MainActivity.this, CallAlerm.class); 168 PendingIntent sender=PendingIntent.getBroadcast( 169 MainActivity.this,1, intent, 0); 170 AlarmManager am; 171 am = (AlarmManager)getSystemService(ALARM_SERVICE); 172 am.set(AlarmManager.RTC_WAKEUP, 173 c.getTimeInMillis(), 174 sender 175 ); 176 String tmpS=format(hourOfDay)+":"+format(minute); 177 setTime3.setText(tmpS); 178 179 //SharedPreferences保存数据,并提交 180 SharedPreferences time2Share = getPreferences(1); 181 SharedPreferences.Editor editor = time2Share.edit(); 182 editor.putString("TIME2", tmpS); 183 editor.commit(); 184 185 Toast.makeText(MainActivity.this,"设置闹钟时间为"+tmpS, 186 Toast.LENGTH_SHORT) 187 .show(); 188 } 189 },mHour,mMinute,true).show(); 190 } 191 }); 192 } 193 194 public void InitButton4() 195 { 196 mButton4=(Button) findViewById(R.id.mButton6); 197 mButton4.setOnClickListener(new View.OnClickListener() 198 { 199 public void onClick(View v) 200 { 201 Intent intent = new Intent(MainActivity.this, CallAlerm.class); 202 PendingIntent sender=PendingIntent.getBroadcast( 203 MainActivity.this,0, intent, 0); 204 AlarmManager am; 205 am =(AlarmManager)getSystemService(ALARM_SERVICE); 206 am.cancel(sender); 207 Toast.makeText(MainActivity.this,"闹钟时间删除", 208 Toast.LENGTH_SHORT).show(); 209 setTime3.setText("目前无设置"); 210 211 //SharedPreferences保存数据,并提交 212 SharedPreferences time2Share = getPreferences(1); 213 SharedPreferences.Editor editor = time2Share.edit(); 214 editor.putString("TIME2", "目前无设置"); 215 editor.commit(); 216 } 217 }); 218 } 219 220 public void InitButton5() 221 { 222 setTime2=(TextView) findViewById(R.id.setTime2); 223 LayoutInflater factory = LayoutInflater.from(this); 224 final View setView = factory.inflate(R.layout.timeset,null); 225 final TimePicker tPicker=(TimePicker)setView 226 .findViewById(R.id.tPicker); 227 tPicker.setIs24HourView(true); 228 229 final AlertDialog di=new AlertDialog.Builder(MainActivity.this) 230 .setIcon(R.drawable.clock) 231 .setTitle("设置") 232 .setView(setView) 233 .setPositiveButton("确定", 234 new DialogInterface.OnClickListener() 235 { 236 public void onClick(DialogInterface dialog, int which) 237 { 238 EditText ed=(EditText)setView.findViewById(R.id.mEdit); 239 int times=Integer.parseInt(ed.getText().toString()) 240 *1000; 241 c.setTimeInMillis(System.currentTimeMillis()); 242 c.set(Calendar.HOUR_OF_DAY,tPicker.getCurrentHour()); 243 c.set(Calendar.MINUTE,tPicker.getCurrentMinute()); 244 c.set(Calendar.SECOND,0); 245 c.set(Calendar.MILLISECOND,0); 246 247 Intent intent = new Intent(MainActivity.this, 248 CallAlerm.class); 249 PendingIntent sender = PendingIntent.getBroadcast( 250 MainActivity.this,1, intent, 0); 251 AlarmManager am; 252 am = (AlarmManager)getSystemService(ALARM_SERVICE); 253 am.setRepeating(AlarmManager.RTC_WAKEUP, 254 c.getTimeInMillis(),times,sender); 255 String tmpS=format(tPicker.getCurrentHour())+":"+ 256 format(tPicker.getCurrentMinute()); 257 String subStr = "设置闹钟时间为"+tmpS+ 258 "开始,重复间隔为"+times/1000+"秒"; 259 setTime2.setText("设置闹钟时间为"+tmpS+ 260 "开始,重复间隔为"+times/1000+"秒"); 261 262 //SharedPreferences保存数据,并提交 263 SharedPreferences time3Share = getPreferences(1); 264 SharedPreferences.Editor editor = time3Share.edit(); 265 editor.putString("TIME3", subStr); 266 editor.commit(); 267 268 Toast.makeText(MainActivity.this,"设置闹钟为"+tmpS+ 269 "开始,重复间隔为"+times/1000+"秒", 270 Toast.LENGTH_SHORT).show(); 271 } 272 }) 273 .setNegativeButton("取消", 274 new DialogInterface.OnClickListener() 275 { 276 public void onClick(DialogInterface dialog, int which) 277 { 278 } 279 }).create(); 280 281 mButton5=(Button) findViewById(R.id.mButton3); 282 mButton5.setOnClickListener(new View.OnClickListener() 283 { 284 public void onClick(View v) 285 { 286 c.setTimeInMillis(System.currentTimeMillis()); 287 tPicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY)); 288 tPicker.setCurrentMinute(c.get(Calendar.MINUTE)); 289 di.show(); 290 } 291 }); 292 } 293 294 public void InitButton6() 295 { 296 mButton6=(Button) findViewById(R.id.mButton4); 297 mButton6.setOnClickListener(new View.OnClickListener() 298 { 299 public void onClick(View v) 300 { 301 Intent intent = new Intent(MainActivity.this, CallAlerm.class); 302 PendingIntent sender = PendingIntent.getBroadcast( 303 MainActivity.this,1, intent, 0); 304 AlarmManager am; 305 am = (AlarmManager)getSystemService(ALARM_SERVICE); 306 am.cancel(sender); 307 Toast.makeText(MainActivity.this,"闹钟时间删除", 308 Toast.LENGTH_SHORT).show(); 309 setTime2.setText("目前无设置"); 310 //SharedPreferences保存数据,并提交 311 SharedPreferences time3Share = getPreferences(2); 312 SharedPreferences.Editor editor = time3Share.edit(); 313 editor.putString("TIME3", "目前无设置"); 314 editor.commit(); 315 } 316 }); 317 } 318 319 @Override 320 public boolean onKeyUp(int keyCode, KeyEvent event) { 321 322 if(keyCode == KeyEvent.KEYCODE_BACK){ 323 builder = new AlertDialog.Builder(MainActivity.this) 324 .setIcon(R.drawable.clock) 325 .setTitle("温馨提示:") 326 .setMessage("您是否要退出闹钟程序!!!") 327 .setPositiveButton("确定", 328 new DialogInterface.OnClickListener() { 329 public void onClick(DialogInterface dialog, 330 int whichButton) { 331 MainActivity.this.finish(); 332 } 333 }) 334 .setNegativeButton("取消", 335 new DialogInterface.OnClickListener() { 336 public void onClick(DialogInterface dialog, 337 int whichButton) { 338 builder.dismiss(); 339 } 340 }).show(); 341 } 342 return true; 343 } 344 345 private String format(int x) 346 { 347 String s=""+x; 348 if(s.length()==1) s="0"+s; 349 return s; 350 } 351 }