標籤:long void drawable copy com 自啟 tin cli wav
簡單的說,這是一個沒有介面的後台啟動並執行應用程式
而且,還有一功能:開機自啟動,啟動運行一個服務
程式結構非常簡單,兩個類,一個是service的擴充類,一個是BroadcastReceiver擴充類
[java]view plain copy print?
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- import android.widget.Toast;
-
- public class TestService extends Service{
-
- public void onCreate(){
- super.onCreate();
- Log.d("AAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAA");
- Toast.makeText(getApplicationContext(), "預設Toast樣式",
- Toast.LENGTH_LONG).show();
- }
-
- public IBinder onBind(Intent intent){
- Log.d("BBBBBBBBBBBBBBBBBBB", "BBBBBBBBBBBBBBBBBBBBBBB");
- Toast.makeText(getApplicationContext(), "不預設Toast樣式",
- Toast.LENGTH_LONG).show();
- return null;
- }
- }
BroadcastReceiver擴充類:
[java]view plain copy print?
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class BootReceiver extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- // TODO Auto-generated method stub
-
- Log.d("WWWWWWWWWWWWWWWWWWWWW", "WWWWWWWWWWWWWWWWWWWWWWWW");
- Intent mBootIntent = new Intent(arg0, TestService.class);
- arg0.startService(mBootIntent);
- Log.d("CCCCCCCCCCCCCCCCCCCCC", "CCCCCCCCCCCCCCCCCCCCCCCC");
- }
- }
設定檔:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.android.com/apk/res/android"
package="com.tyq"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:name=".TestService"></service>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="10" />
</manifest>
參考http://netspook.iteye.com/blog/1291236
附上android廣播相關知識
http://bk-lin.iteye.com/blog/1569568
Android -- 開機啟動無介面背景程式