2011-09-29 75 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),因此必须有更好的理由来正确地绑定它们。 –

相关问题