Creative Commons License 本站採用CC 姓名標示-非商業性-禁止改作3.0台灣條款|||本部落格中的文章含蓋科技,程式設計,電腦網路,及我的日記...等等。

2011年4月30日星期六

Android專案第二部分非常不順利..

我在寫某Android程式,有一部分是要隱藏STATUSBAR,但是卡到瓶頸.

因為STATUSBARMANAGER是系統的API,他不包含在SDK裡面,所以調用時一直失敗,之後我改用Reflection,程式碼大致如下(這部分是展開狀態列):
Method expandStatusBar;
Object mStatusBarManager= new Object();
Object statusBarManager = new Object();
public void onItemClick(AdapterView arg0,
View arg1, int arg2, long arg3) {
this.setTitle("You Click Item:" + String.valueOf(arg2));

mStatusBarManager=this.getSystemService("STATUS_BAR");
Method[] methods=statusBarManager.getClass().getDeclaredMethods();
for (Method method : methods)
{
if (method.getName().compareTo("expand")==0)
expandStatusBar = method;
}
try {
expandStatusBar.invoke(statusBarManager);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
 結果又卡住了,上面的for迴圈裡面的if (method.getName().compareTo("expand")==0)完全跑不進去,想了幾天想不到,乾脆去發問,結果發現我沒有在Manifast設android:permission...

開心地設了    <uses-permission android:name="android.permission.STATUS_BAR" />
 android:permission="android.permission.STATUS_BAR">> 之後,馬上 compile!結果出現錯誤,java.lang.SecurityException: Permission Denial: starting Intent
{ act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000
cmp=com.xxxx/.xxxx } from null (pid=-1, uid=-1) requires android.permission.STATUS_BAR

這讓我無言了,我乾脆跑去美國問啦,問題內容如下
I used

<uses-permission android:name="android.permission.STATUS_BAR" />
 <activity android:name=".xxxx"
        android:permission="android.permission.STATUS_BAR">




When I compile,the error occurred..

[2011-04-29 20:37:30 - xxxx] ActivityManager:
java.lang.SecurityException: Permission Denial: starting Intent
{ act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER] flg=0x10000000
cmp=com.xxxx/.xxxx } from null (pid=-1, uid=-1) requires
android.permission.STATUS_BAR

Why!?
得到回答是..
You are trying to use a permission that applications aren't allowed to. You are then saying that launching your activity requires that the caller hold this permission, which no app (including the launcher) is going to.

SDK applications cannot hold that permission, AFAIK, and I don't know
why you have an activity that requires that permission.

靠杯,我才發現這禮拜我都在搞笑,原來就算用Reflection調到系統Service,沒permission還是一場空。

浪費了一個禮拜,改變計畫。


後記:由於Launcher類軟體是脫離SDK的,所以他可以輕易的調用到STATUSBARMANGER..

0 意見:

張貼意見

留言不須審核,但需輸入驗證碼

延伸閱讀