導航:首頁 > 操作系統 > 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開發相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:617
php5314 瀏覽:358
完美國際安裝到哪個文件夾 瀏覽:669
什麼app可以掃一掃做題 瀏覽:540
程序員編碼論壇 瀏覽:924
淘點是什麼app 瀏覽:660
中國高等植物pdf 瀏覽:454
51單片機時間 瀏覽:182
後台如何獲取伺服器ip 瀏覽:267
單片機流水燈程序c語言 瀏覽:236
程序員第二職業掙錢 瀏覽:240
運行里怎麼輸入伺服器路徑 瀏覽:843
pythonstepwise 瀏覽:512
劉一男詞彙速記指南pdf 瀏覽:66
php認證級別 瀏覽:371
方舟編譯啥時候推送 瀏覽:1012
php手機驗證碼生成 瀏覽:677
哲學思維pdf 瀏覽:17
凌達壓縮機有限公司招聘 瀏覽:535
weblogic命令部署 瀏覽:39