導航:首頁 > 操作系統 > androidservice開發

androidservice開發

發布時間:2024-11-08 10:09:10

android開發 service 和activity 廣播問題

這里我們先假定service發出內容時候的Action為ActionS。


如果activity里沒有動態注冊監聽service發出的ActionS的廣播, 即使Activity當前在使用中也不會得到通知, 更不用說未啟動的Activity來捕獲這個通知了。


要捕獲這個字元串有兩種方式, 分別如下

  1. 在AndroidManifest.xml中注冊
    <receiver android:name="YourBroadcastReceiver" >
    <intent-filter>
    <action android:name="ActionS" />
    </intent-filter>
    </receiver>
    這樣, 一旦有定義的ActionS發出來,YourBroadcastReceiver的onReceive方法就會回調了,這樣的監聽,不需要你的app已經在運行。你在onReceive方法里攔截處理。


2.在Activity中動態創建監聽器, onCreate()中生成一個IntentFilter對象

IntentFilter filter=new IntentFilter();
//為IntentFilter添加一個ActionS

filter.addAction(ActionS);
yourBroadcastReceiver = newYourBroadcastReceiver();

registerReceiver(yourBroadcastReceiver, filter);


在onDestroy的時候去注冊
unregisterReceiver(yourBroadcastReceiver);
這樣的方式只有在Activity生命周期onCreate()-onDestroy()之間有效, 在YourBroadcastReceiver.onReceive()方法里攔截處理。

⑵ Android開發怎麼調試Service

Android開發如何調試Service

Android 開發中,添加代碼對Service 進行調試 。

介紹
以調試 模式啟動Android 項目時,在service 中設置斷點,調試 器不會停止下來
解決方法
所有的這種情況下,都是在代碼中聲明。調用的方法是:

android.os.Debug.waitForDebugger();

舉個例子,SoftKeyboard:

public class SoftKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { @Override public void onConfigurationChanged(Configuration newConfig) { Log.d("SoftKeyboard", "onConfigurationChanged()"); /* now let's wait until the debugger attaches */ android.os.Debug.waitForDebugger(); super.onConfigurationChanged(newConfig); /* do something useful... */ }

代碼中你可以看到,首先是調用了日誌記錄器logger,代碼運行到這里時,會將在logcat中添加一條記錄,這是跟蹤代碼運行的一種方法,如果不需要在斷點上停止時可以使用。但通常為了更詳細的調試 ,這是不足夠的。
第二條語句等待添加調試 器,添加了這條語句之後,可以在這個方法的任何地方添加斷點。
Activity也是應用的部分時調試 Service 就更加容易了。那種情況下,首先需要啟動Activity,調試 器也可以在Service 的斷點中停止下來,不需要調用 waitForDebugger()。

⑶ 怎麼開發android service 開機啟動

第一步:首先創建一個廣播接收者,重構其抽象方法 onReceive(Context context, Intent intent),在其中啟動你想要啟動的Service或app。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootBroadcastReceiver extends BroadcastReceiver {
//重寫onReceive方法
@Override
public void onReceive(Context context, Intent intent) {
//後邊的XXX.class就是要啟動的服務
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "開機自動服務自動啟動.....");
//啟動應用,參數為需要自動啟動的應用的包名
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
}

}
第二步:配置xml文件,在receiver接收這種添加intent-filter配置
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
第三步:添加許可權 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

閱讀全文

與androidservice開發相關的資料

熱點內容
酒店怎麼使用警察叔叔app 瀏覽:76
java數組怎麼輸入 瀏覽:383
java新建對象數組 瀏覽:146
aps系統排產EXCEL源碼 瀏覽:184
cnc學什麼編程語言 瀏覽:877
多元邏輯回歸演算法的矩陣 瀏覽:2
地鐵逃生體驗服顯示伺服器異常怎麼辦 瀏覽:323
14乘17的速演算法 瀏覽:346
小型泡沫壓縮機 瀏覽:104
php上傳的文件名亂碼 瀏覽:996
2017賀銀成講義pdf 瀏覽:854
麻將分演算法 瀏覽:589
安卓手機如何打開sdl文件 瀏覽:215
為什麼安卓手機截不了收付款的圖 瀏覽:242
賤人插件命令 瀏覽:843
單片機發展論文 瀏覽:316
條件編譯和符號幹嘛用的 瀏覽:85
程序員坐牢了會被安排去寫代碼嗎 瀏覽:855
iphoneapp使用時間怎麼關閉 瀏覽:431
怎麼建造生存伺服器指令 瀏覽:554