2011-09-29 74 views
1

嘿所有安裝我的應用程序模擬器,當我得到這個:java.lang.SecurityException異常:不允許啓動服務意向

ERROR/AndroidRuntime(465):了java.lang.RuntimeException:無法啓動接收器com.myPackage.Widget.MYWidget:java.lang.SecurityException異常:不允許啓動服務意向{CMP = com.myPackage/.Widget.MYWidget $ MyWidgetService}擅自android.permission.BIND_REMOTEVIEWS

這裏我清單中的代碼

<!-- Broadcast Receiver that will process AppWidget updates --> 
    <receiver android:name=".Widget.MYWidget" android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 

      <!--Broadcast Receiver that will also process our self created action --> 
      <action android:name="com.temp.package.Widget.MYWidget.ACTION_WIDGET_LEFT_RECEIVER" /> 


      <action android:name="com.temp.package.Widget.MYWidget.ACTION_WIDGET_PROGRESSBAR_RECEIVER" /> 
     </intent-filter> 
     <meta-data android:name="android.appwidget.provider" 
      android:resource="@xml/mywidget_widget_provider" /> 

    </receiver> 
    <service android:name=".Widget.MYWidget$MyWidgetService" 
     android:permission="android.permission.BIND_REMOTEVIEWS" 
     android:exported="true" /> 
     <uses-permission 
    android:name="android.permission.BIND_REMOTEVIEWS"></uses-permission> 

這是我的代碼

public class MyWidget extends AppWidgetProvider { 
@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

    Intent svcIntent = new Intent(context, MyWidgetService.class); 

    //svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME))); 
    context.startService(svcIntent); 
} 

public static class MyWidgetService extends Service 
{ 
    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     super.onStart(intent, startId); 
     // Update the widget 
     RemoteViews remoteView = buildRemoteView(this); 

     // Push update to homescreen 
     pushUpdate(remoteView); 

     // No more updates so stop the service and free resources 
     stopSelf(); 
    } 

    public RemoteViews buildRemoteView(Context context) 
    { 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 

     //do stuff 


     return remoteViews; 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) 
    { 
     int oldOrientation = this.getResources().getConfiguration().orientation; 

     if(newConfig.orientation != oldOrientation) 
     { 
      // Update the widget 
      RemoteViews remoteView = buildRemoteView(this); 

      // Push update to homescreen 
      pushUpdate(remoteView); 
     } 
    } 

    private void pushUpdate(RemoteViews remoteView) 
    { 
     ComponentName myWidget = new ComponentName(this, MyWidget.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 
     manager.updateAppWidget(myWidget, remoteView); 
    } 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

回答

6

擺脫:

android:permission="android.permission.BIND_REMOTEVIEWS" 
    android:exported="true" 
<service>元素

,因爲這裏既不需要。這應該清除你的問題。

+0

@Elad Gelman:我不知道你在說什麼,它似乎與這個問題沒有關係。請考慮開始一個新的問題並解釋更多。 – CommonsWare

0

它幾乎可以工作,當我添加Widget時,我處於垂直模式。發生方向變化時(水平方向) - 我認爲onConfigurationChanged應該被調用,但它不是 - 因此我的部件不工作後

爲此,你需要提及你想要的android:configChanges參數處理你的。 請不要,如果你在ICS代碼上工作,你需要把'screenSize'元素與其他參數'keyboardHidden | screenSize' 希望這會幫助你。

我只提到了這個活動...我錯誤地提出了你的問題。 感謝您的更新。

+0

我認爲你錯了,只在活動上調用configchanges,這是一個正在運行的服務。 Android Widgets存在問題 - 每次從主屏幕調用configChanges時,整個UI元素都將被銷燬並重建(Widgets),因此必須有更好的理由來正確地綁定它們。 –

相關問題