最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android開發(fā)教程之開機(jī)啟動服務(wù)service示例

 更新時間:2014年03月06日 14:38:08   作者:  
如果開機(jī)啟動一個Activity,開機(jī)首先看的界面,是你的程序界面,如果為了,開機(jī)后也啟動你的程序,但是不顯示自己程序的界面,就要用Service服務(wù),下面是開機(jī)啟動服務(wù)service示例

個例子實現(xiàn)的功能是:
1,安裝程序后看的一個Activity程序界面,里面有個按鈕,點擊按鈕就會啟動一個Service服務(wù),此時在設(shè)置程序管理里面會看的有個Activity和一個Service服務(wù)運行
2,如果手機(jī)關(guān)機(jī)重啟,會觸發(fā)你的程序里面的Service服務(wù),當(dāng)然,手機(jī)啟動后是看不到你的程序界面。好比手機(jī)里面自帶的鬧鐘功能,手機(jī)重啟看不到鬧鐘設(shè)置界面
只是啟動服務(wù),時間到了,鬧鐘就好響鈴提醒。

程序代碼是:

首先要有一個用于開機(jī)啟動的Activity,給你們的按鈕設(shè)置OnClickListener();

復(fù)制代碼 代碼如下:

public class MainActivity extends Activity {
 private Button btnstarted = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     btnstarted = (Button)findViewById( R.id.btnstarted);
     btnstarted.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this,StartService.class);
    startService(intent);
    Toast.makeText(MainActivity.this, "服務(wù)啟動成功", Toast.LENGTH_LONG).show();
   }};}
}

我們要編寫一個BroadcastReceiver用以捕獲ACTION_BOOT_COMPLETED這條廣播,并在捕獲之后啟動我們要啟動的服務(wù)StarServie.class

復(fù)制代碼 代碼如下:

public class BootCompletedReceiver extends BroadcastReceiver{
 public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
   Intent newIntent = new Intent(context,StartService.class);
   context.startService(newIntent);
  }
 }
}

啟動服務(wù)Service代碼

復(fù)制代碼 代碼如下:

public class StartService extends Service{
 //public static String PHONENO;

 public class LocalBinder extends Binder{
  StartThief getService(){
   return StartService.this;
  }
 }
 public IBinder onBind(Intent intent){
  return mBinder;
 }
 private void registerIntentReceiver(){
  //此處添加啟動服務(wù)要執(zhí)行的操作代碼
 }
 public void onStart(Intent intent,int startId){
  super.onStart(intent, startId);
 }
    @Override
    public void onCreate() {
  registerIntentReceiver();
    }
}

用到的Main.xml,里面只有一個Button ,id是btnstarted

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
 android:id="@+id/AbsoluteLayout01"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <Button android:layout_height="wrap_content"
 android:id="@+id/btnstarted"
 android:text="@string/started"
 android:layout_y="118dip"
 android:layout_width="wrap_content"
 android:layout_x="56dip">
 </Button>
</AbsoluteLayout>

在AndroidManifest.xml配置文件中注冊我們的BroadcastReceiver和服務(wù)Service

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.thief" android:versionCode="1"
 android:versionName="1.0">
 <application android:icon="@drawable/icon"
  android:label="@string/app_name">
  <activity android:name=".MainActivity"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category
     android:name="android.intent.category.LAUNCHER" />
   </intent-filter> 
  </activity>
         //注冊服務(wù)
                <service android:name=".StartService"></service>
                //為了獲取開機(jī)啟動這個動作,必須注冊加上android.intent.action.BOOT_COMPLETED
  <receiver android:name=".BootCompletedReceiver">
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED">        
    </action>
   </intent-filter>
  </receiver> 
 </application>
 獲取開機(jī)啟動動作的權(quán)限permission
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>"
</manifest>

相關(guān)文章

最新評論

玛多县| 阳山县| 安图县| 简阳市| 榆中县| 会同县| 三门县| 峨山| 台湾省| 崇明县| 宣化县| 思茅市| 南部县| 鹤峰县| 印江| 健康| 沅陵县| 乌拉特后旗| 康定县| 崇明县| 汨罗市| 南皮县| 休宁县| 武定县| 白山市| 永善县| 嘉定区| 河南省| 北京市| 天门市| 西乡县| 平泉县| 汪清县| 泉州市| 周口市| 德保县| 娄底市| 延川县| 宁波市| 古田县| 余干县|