java.lang.SecurityException:不允许启动服务意图
嘿大家当我将我的应用程序安装到模拟器时我得到这个:
错误/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;
}
}
hey all when installing my app to the emulator I get this:
ERROR/AndroidRuntime(465): java.lang.RuntimeException: Unable to start receiver com.myPackage.Widget.MYWidget: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.myPackage/.Widget.MYWidget$MyWidgetService } without permission android.permission.BIND_REMOTEVIEWS
here is the code from my manifest
<!-- 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>
this is my code
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除:
从您的
元素中 ,因为这里都不需要。这应该可以解决你的问题。Get rid of:
from your
<service>
element, as neither are needed here. This should clear up your problem.它几乎可以工作,当我添加小部件时我处于垂直模式。当方向发生变化时(水平方向) - 我认为应该调用 onConfigurationChanged 但事实并非如此 - 因此我的小部件在之后无法工作
为此,您需要提及要处理的 android:configChanges 参数你的。
如果您正在处理 ICS 代码,请不要将“screenSize”元素与“keyboardHidden|screenSize”等其他参数一起放置在那里
希望这会对您有所帮助。
我提到这一点只是为了活动......我错误地理解了你的问题。
感谢您的更新。
it's almost working, I'm in vertical mode when I add the Widget. when on orientation change happens(To Horizontal) - I thought that onConfigurationChanged should be called but it is not- thus my widget is not working after
For this you need to mention the android:configChanges parameter for which you want to handle your on.
Please not if you working on ICS code you need to put 'screenSize' element also there with other parameters like 'keyboardHidden|screenSize'
Hope this will help you.
I mentioned this for activity only...I took your question wrongly.
Thanks for update.