java.lang.SecurityException:不允许启动服务Intent

时间:2011-09-29 19:35:43

标签: android service widget remoteview

嘿,当我将我的应用程序安装到模拟器时,我得到了这个:

  

错误/ AndroidRuntime(465):java.lang.RuntimeException:无法启动接收器com.myPackage.Widget.MYWidget:java.lang.SecurityException:不允许启动服务Intent {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;
    }
}

2 个答案:

答案 0 :(得分:6)

摆脱:

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

,因为这里都不需要。这应该可以解决你的问题。

答案 1 :(得分:0)

它几乎正常工作,当我添加Widget时,我处于垂直模式。当方向改变发生时(To Horizo​​ntal) - 我认为应该调用onConfigurationChanged但是它不是 - 因此我的小部件在之后无效

为此你需要提到你想要处理你的android:configChanges参数。 请不要,如果您正在使用ICS代码,您需要将'screenSize'元素与其他参数放在一起,例如'keyboardHidden | screenSize' 希望这会对你有所帮助。

我之所以提到这只是为了活动......我错误地提出了你的问题。 感谢您的更新。