欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

程序代碼是:

首先要有一個(gè)用于開(kāi)機(jī)啟動(dòng)的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ù)啟動(dòng)成功", Toast.LENGTH_LONG).show();
   }};}
}

我們要編寫一個(gè)BroadcastReceiver用以捕獲ACTION_BOOT_COMPLETED這條廣播,并在捕獲之后啟動(dòng)我們要啟動(dòng)的服務(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);
  }
 }
}

啟動(dòng)服務(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(){
  //此處添加啟動(dòng)服務(wù)要執(zhí)行的操作代碼
 }
 public void onStart(Intent intent,int startId){
  super.onStart(intent, startId);
 }
    @Override
    public void onCreate() {
  registerIntentReceiver();
    }
}

用到的Main.xml,里面只有一個(gè)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配置文件中注冊(cè)我們的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>
         //注冊(cè)服務(wù)
                <service android:name=".StartService"></service>
                //為了獲取開(kāi)機(jī)啟動(dòng)這個(gè)動(dòng)作,必須注冊(cè)加上android.intent.action.BOOT_COMPLETED
  <receiver android:name=".BootCompletedReceiver">
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED">        
    </action>
   </intent-filter>
  </receiver> 
 </application>
 獲取開(kāi)機(jī)啟動(dòng)動(dòng)作的權(quán)限permission
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>"
</manifest>

相關(guān)文章

最新評(píng)論