android開發(fā)教程之開機啟動服務(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();
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
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代碼
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
<?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
<?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使用surfaceView自定義抽獎大轉(zhuǎn)盤
這篇文章主要為大家詳細介紹了Android使用surfaceView自定義抽獎大轉(zhuǎn)盤,熟練掌握SurfaceVie實現(xiàn)抽獎大轉(zhuǎn)盤,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Android開發(fā)學(xué)習(xí)實現(xiàn)簡單計算器
這篇文章主要為大家詳細介紹了Android實現(xiàn)一個簡單計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04Android apk 項目一鍵打包并上傳到蒲公英的實現(xiàn)方法
這篇文章主要介紹了Android apk 項目一鍵打包并上傳到蒲公英,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Android編程獲取系統(tǒng)隱藏服務(wù)實現(xiàn)鎖屏的方法
這篇文章主要介紹了Android編程獲取系統(tǒng)隱藏服務(wù)實現(xiàn)鎖屏的方法,涉及Android關(guān)于廣播,服務(wù),權(quán)限及鎖屏等操作的相關(guān)技巧,需要的朋友可以參考下2015-12-12Android eclipse使用gradle打包的圖文教程
本文通過圖文并茂的形式給大家介紹了Android eclipse使用gradle打包的方法,需要的朋友可以參考下2018-10-10Android TimePicker 直接輸入的問題解決方案
這篇文章主要介紹了Android TimePicker 直接輸入的問題解決方案的相關(guān)資料,需要的朋友可以參考下2017-04-04