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

熱點內容
編程語言的優點與缺點 瀏覽:978
台灣歷史pdf 瀏覽:824
貸款簡訊提醒源碼 瀏覽:114
喬家的兒女在什麼app播 瀏覽:340
javalicense實現 瀏覽:376
mysql創建資料庫命令 瀏覽:990
紅色的魚是什麼app 瀏覽:685
程序員的生涯經歷 瀏覽:437
納粹命令 瀏覽:590
什麼講鬼故事app 瀏覽:969
程序員級升 瀏覽:89
怎樣關閉照片加密 瀏覽:524
文件夾變拉鏈 瀏覽:609
伺服器未在運行什麼意思 瀏覽:409
單片機應用大賽 瀏覽:466
博格上海壓縮機有限公司 瀏覽:29
招行車貸解壓有費用嗎 瀏覽:704
總統命令小說 瀏覽:820
安卓手機為什麼卡成狗 瀏覽:386
廣州市公司軟體加密 瀏覽:232