android開(kāi)發(fā)教程之開(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();
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
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代碼
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
<?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
<?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>
- android?Service基礎(chǔ)(啟動(dòng)服務(wù)與綁定服務(wù))
- Android ServiceManager的啟動(dòng)和工作原理
- Android 系統(tǒng)服務(wù)TelecomService啟動(dòng)過(guò)程原理分析
- Android Service的啟動(dòng)過(guò)程分析
- Android Service啟動(dòng)過(guò)程完整分析
- Android實(shí)現(xiàn)開(kāi)機(jī)自動(dòng)啟動(dòng)Service或app的方法
- Android Service自啟動(dòng)注意事項(xiàng)分析
- Android中實(shí)現(xiàn)開(kāi)機(jī)自動(dòng)啟動(dòng)服務(wù)(service)實(shí)例
- Android?Service啟動(dòng)流程刨析
相關(guān)文章
Android獲取、更改包名的小技巧分享(超實(shí)用)
這篇文章主要給大家分享介紹了關(guān)于利用Android更改包名的一個(gè)小技巧,通過(guò)這個(gè)方法大家可以很方便的修改包名,再也不用為頻繁修改包名而感到頭疼,文中還給大家分享利一個(gè)android獲取手機(jī)所有應(yīng)用包名的方法,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12kotlin中object關(guān)鍵字的三種使用場(chǎng)景
這篇文章主要給大家介紹了關(guān)于kotlin中object關(guān)鍵字的三種使用場(chǎng)景,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Android使用surfaceView自定義抽獎(jiǎng)大轉(zhuǎn)盤
這篇文章主要為大家詳細(xì)介紹了Android使用surfaceView自定義抽獎(jiǎng)大轉(zhuǎn)盤,熟練掌握SurfaceVie實(shí)現(xiàn)抽獎(jiǎng)大轉(zhuǎn)盤,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android開(kāi)發(fā)學(xué)習(xí)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04Android apk 項(xiàng)目一鍵打包并上傳到蒲公英的實(shí)現(xiàn)方法
這篇文章主要介紹了Android apk 項(xiàng)目一鍵打包并上傳到蒲公英,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Android編程獲取系統(tǒng)隱藏服務(wù)實(shí)現(xiàn)鎖屏的方法
這篇文章主要介紹了Android編程獲取系統(tǒng)隱藏服務(wù)實(shí)現(xiàn)鎖屏的方法,涉及Android關(guān)于廣播,服務(wù),權(quán)限及鎖屏等操作的相關(guān)技巧,需要的朋友可以參考下2015-12-12Android eclipse使用gradle打包的圖文教程
本文通過(guò)圖文并茂的形式給大家介紹了Android eclipse使用gradle打包的方法,需要的朋友可以參考下2018-10-10Android TimePicker 直接輸入的問(wèn)題解決方案
這篇文章主要介紹了Android TimePicker 直接輸入的問(wèn)題解決方案的相關(guān)資料,需要的朋友可以參考下2017-04-04