建设银行网站怎么取消短信服务,指数计算器,贷款平台推广代理,wordpress 边框? Activity 好像是應用程式的眼睛#xff0c;提供與User 互動之窗。 ? BroadcastReceiver 好像是耳朵#xff0c;接收來自各方的Intent。 ? Service 好像是手#xff0c;提供符合Intent 意圖之服務。 10.2.1 操作情境#xff1a; 1. 此程式一開始#xff0c;畫面出現兩…? Activity 好像是應用程式的眼睛提供與User 互動之窗。 ? BroadcastReceiver 好像是耳朵接收來自各方的Intent。 ? Service 好像是手提供符合Intent 意圖之服務。 10.2.1 操作情境 1. 此程式一開始畫面出現兩個按鈕如下 2. 按下call_service按鈕暫停15 秒 3. 等待15 秒後委託Alarm Manager 發出intent。當BroadcastReceiver 接到intent時就啟動NotifyService此服務會回傳字串顯示於ac01 畫面的Title 區域 4. 按下Exit程式就結束了。 10.2.2 撰寫步驟Step-1: 建立Android 專案kx02。Step-2: 撰寫BroadcastReceiver 的子類別AlarmReceiver其程式碼如下、 package com.misoo.kx02;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
Override public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, NotifyService.class));
}} Step-3: 撰寫Service 的子類別NotifyService其程式碼如下 package com.misoo.kx02;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class NotifyService extends Service{
Override protected void onCreate() {
ac01 app ac01.getApp();
app.btEvent(from NotifyService); }
Override public IBinder onBind(Intent intent) { return null; }
} Step-4: 撰寫Activity 的子類別ac01其程式碼如下 package com.misoo.kx02;
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.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class ac01 extends Activity implements OnClickListener{
private static ac01 appRef null;
private Button btn, btn2; boolean k false;
Override protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
appRef this; setContentView(R.layout.cs);
btn (Button)findViewById(R.id.call_service);
btn.setOnClickListener(this);
btn2 (Button)findViewById(R.id.exit);
btn2.setOnClickListener(this);
}
public static ac01 getApp() { return appRef; }
public void btEvent( String data ) { setTitle(data); }
public void onClick(View arg0) {
if(arg0 btn){
setTitle(Waiting... Alarm15);
Intent intent new Intent(ac01.this, AlarmReceiver.class);
PendingIntent p_intent PendingIntent.getBroadcast(ac01.this, 0, intent, 0);
Calendar calendar Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 15);
// Schedule the alarm!
AlarmManager am (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), p_intent); }
if(arg0 btn2){
Intent intent new Intent(ac01.this, AlarmReceiver.class);
PendingIntent p_intent PendingIntent.getBroadcast(ac01.this, 0, intent, 0);
AlarmManager am (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(p_intent); finish();
}}} Step-5: 修改AndroidManifest.xml 的內容更改為 ?xml version1.0 encodingutf-8?
manifest xmlns:androidhttp://schemas.android.com/apk/res/android
packagecom.misoo.kx02
application android:icondrawable/icon
activity android:name.ac01 android:labelstring/app_name
intent-filter
action android:nameandroid.intent.action.MAIN /
category android:nameandroid.intent.category.LAUNCHER /
/intent-filter
/activity
receiver android:name.AlarmReceiver
/receiver
service android:name.NotifyService
/service
/application
/manifest 由Activity发送过来一个消息然后BroadcastReceiver收到再发送intent去启动Service。 onBind负责intent的接收。Service的任务就是调用btEvent这个方法。getApp是一个Activity的方法主要是负责得到那个Activity的Context。 Activity向BroadcastReceiver发送消息然后Receiver收到消息之后发送Intent去ServiceService就会根据消息作出一些影响UI的行动。Service实际就是一个后台运行的Activity。转载于:https://www.cnblogs.com/Tammie/archive/2012/08/10/2632056.html