亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

程序代碼是:

首先要有一個用于開機啟動的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>
                //為了獲取開機啟動這個動作,必須注冊加上android.intent.action.BOOT_COMPLETED
  <receiver android:name=".BootCompletedReceiver">
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED">        
    </action>
   </intent-filter>
  </receiver> 
 </application>
 獲取開機啟動動作的權(quán)限permission
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>"
</manifest>

相關(guān)文章

  • Android中三種注入事件方法比較

    Android中三種注入事件方法比較

    這篇文章主要介紹了Android中三種注入事件方法比較,本文分別講解了使用內(nèi)部APIs、使用instrumentation對象、直接注入事件到設(shè)備/dev/input/eventX等3種方法,需要的朋友可以參考下
    2015-02-02
  • Android獲取、更改包名的小技巧分享(超實用)

    Android獲取、更改包名的小技巧分享(超實用)

    這篇文章主要給大家分享介紹了關(guān)于利用Android更改包名的一個小技巧,通過這個方法大家可以很方便的修改包名,再也不用為頻繁修改包名而感到頭疼,文中還給大家分享利一個android獲取手機所有應(yīng)用包名的方法,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Android6.0 消息機制原理解析

    Android6.0 消息機制原理解析

    這篇文章主要為大家詳細介紹了Android6.0 消息機制原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • kotlin中object關(guān)鍵字的三種使用場景

    kotlin中object關(guān)鍵字的三種使用場景

    這篇文章主要給大家介紹了關(guān)于kotlin中object關(guān)鍵字的三種使用場景,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Android使用surfaceView自定義抽獎大轉(zhuǎn)盤

    Android使用surfaceView自定義抽獎大轉(zhuǎn)盤

    這篇文章主要為大家詳細介紹了Android使用surfaceView自定義抽獎大轉(zhuǎn)盤,熟練掌握SurfaceVie實現(xiàn)抽獎大轉(zhuǎn)盤,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android開發(fā)學(xué)習(xí)實現(xiàn)簡單計算器

    Android開發(fā)學(xué)習(xí)實現(xiàn)簡單計算器

    這篇文章主要為大家詳細介紹了Android實現(xiàn)一個簡單計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Android apk 項目一鍵打包并上傳到蒲公英的實現(xiàn)方法

    Android apk 項目一鍵打包并上傳到蒲公英的實現(xiàn)方法

    這篇文章主要介紹了Android apk 項目一鍵打包并上傳到蒲公英,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Android編程獲取系統(tǒng)隱藏服務(wù)實現(xiàn)鎖屏的方法

    Android編程獲取系統(tǒng)隱藏服務(wù)實現(xiàn)鎖屏的方法

    這篇文章主要介紹了Android編程獲取系統(tǒng)隱藏服務(wù)實現(xiàn)鎖屏的方法,涉及Android關(guān)于廣播,服務(wù),權(quán)限及鎖屏等操作的相關(guān)技巧,需要的朋友可以參考下
    2015-12-12
  • Android eclipse使用gradle打包的圖文教程

    Android eclipse使用gradle打包的圖文教程

    本文通過圖文并茂的形式給大家介紹了Android eclipse使用gradle打包的方法,需要的朋友可以參考下
    2018-10-10
  • Android TimePicker 直接輸入的問題解決方案

    Android TimePicker 直接輸入的問題解決方案

    這篇文章主要介紹了Android TimePicker 直接輸入的問題解決方案的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論