package com.example.administrator.mytestapp.BroadcastDemo; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.icu.util.Calendar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.example.administrator.mytestapp.R; public class TimeAlarmActivity extends AppCompatActivity { private Button BtnStartTimerAlarm,BtnExit; private static TimeAlarmActivity App; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_time_alarm); BtnStartTimerAlarm= (Button) findViewById(R.id.BtnStartTimerAlarm); BtnExit= (Button) findViewById(R.id.BtnExit); BtnStartTimerAlarm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(TimeAlarmActivity.this, "等待...闹钟倒计时为3秒", Toast.LENGTH_SHORT).show(); Intent intent=new Intent("android.intent.action.EDIT" ); PendingIntent pi=PendingIntent.getBroadcast(TimeAlarmActivity.this,0,intent,0); java.util.Calendar c= java.util.Calendar.getInstance(); c.add(java.util.Calendar.SECOND,3); AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi); } }); BtnExit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(TimeAlarmActivity.this,TimerAlarmReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(TimeAlarmActivity.this,0,intent,0); AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE); am.cancel(pi); finish(); } }); } public static TimeAlarmActivity getApp() { return App; } // }
package com.example.administrator.mytestapp.BroadcastDemo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class TimerAlarmReceiver extends BroadcastReceiver { public TimerAlarmReceiver() { Log.e("TAG","Receiver被实例化"); } @Override public void onReceive(Context context, Intent intent) { context.startService(new Intent(context,TimerAlarmService.class)); context.stopService(new Intent(context,TimerAlarmService.class)); } }
package com.example.administrator.mytestapp.BroadcastDemo; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class TimerAlarmService extends Service { public TimerAlarmService() { Log.e("TAG","Service被实例化"); } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { TimeAlarmActivity app=TimeAlarmActivity.getApp(); // app.setTitle("来自于定时闹钟服务"); Log.e("TAG","开启闹钟提醒"); Toast.makeText(this, "定时闹钟提醒", Toast.LENGTH_LONG).show(); super.onCreate(); } @Override public void onDestroy() { String AppName="TimerAlarm"; TimeAlarmActivity app=TimeAlarmActivity.getApp(); //app.setTitle(AppName); super.onDestroy(); } }
<activity android:name=".BroadcastDemo.TimeAlarmActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BroadcastDemo.TimerAlarmReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.intent.action.EDIT" /> </intent-filter> </receiver> <service android:name=".BroadcastDemo.TimerAlarmService" android:enabled="true" android:exported="true"></service> </application>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.mytestapp.BroadcastDemo.TimeAlarmActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/BtnStartTimerAlarm" android:text="开启定时闹钟服务"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/BtnExit" android:text="退出"/> </LinearLayout>