android 5 0 遠端開啟服務學習筆記

2021-07-03 11:11:49 字數 1445 閱讀 2085

有些時候我們使用service的時需要採用隱私啟動的方式,但是android 5.0一出來後,其中有個特性就是

service intent  must be explitict

,也就是說從lollipop開始,service服務必須採用顯示方式啟動。

private void validateserviceintent(intent service) else }}

複製**

既然,原始碼裡是這樣寫的,那麼這裡有兩種解決方法:

1、設定action和packagename:

參考**如下:

intent mintent = new intent();

mintent.setaction("***.***.***");//你定義的service的action

mintent.setpackage(getpackagename());//這裡你需要設定你應用的包名

context.startservice(mintent);

複製**

此方式是google官方推薦使用的解決方法。

2、將隱式啟動轉換為顯示啟動

:public static intent getexplicitintent(context context, intent implicitintent)

// get component info and create componentname

resolveinfo serviceinfo = resolveinfo.get(0);

string packagename = serviceinfo.serviceinfo.packagename;

string classname = serviceinfo.serviceinfo.name;

componentname component = new componentname(packagename, classname);

// create a new intent. use the old one for extras and such reuse

intent explicitintent = new intent(implicitintent);

// set the component to be explicit

explicitintent.setcomponent(component);

return explicitintent;}

複製**

呼叫方式如下:

intent mintent = new intent();

mintent.setaction("***.***.***");

intent eintent = new intent(getexplicitintent(mcontext,mintent));

context.startservice(eintent);

複製**

Android5 0新控制項

1.recycleview 使用方法 實現listview,gridview及瀑布流 新增分割線 自定義itemderection 刪除及增加動畫 下拉重新整理 onscrollstatechanged 引數 recyclerview recyclerview,int newstate 正在滾動 p...

android 5 0 修改預設桌布

android 5.0中homescreen的預設桌布位置 frameworks base core res res drawable nodpi default wall jpg 可以在frameworks base core res目錄下搜尋default wall jpg檔案,替換掉就可以了。...

android5 0的過度動畫

最近一直研究5.0的過度動畫,網上找了好多帖子 博文,千篇一律,乙個博文或者乙個帖子,引申出好多,然並卵,基本上都說的是一共分成兩種方法 一,實現 fade fade new fade fade.setduration 2000 getwindow setreentertransition fade...