【问题标题】:Security Exception: Not allowed to start a service Intent安全异常:不允许启动服务 Intent
【发布时间】:2013-06-14 17:31:30
【问题描述】:

好的,我是 android 开发新手,所以请多多包涵。我正在关注学习 android 书,并且遇到了刷新服务无法工作的问题,因为它没有必要的权限。谁能告诉我是什么导致了这个问题? logcat 抛出运行时错误:*java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.yamba/.RefreshService } without permission com.example.yamba.permission.REFRESH at com.example.yamba.TimelineActivity.onOptionsItemSelected(TimelineActivity.java:138) *

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yamba"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.example.yamba.permission.REFRESH" />

<application
    android:name=".YambaApp"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.example.yamba.StatusActivity"
        android:configChanges="orientation"
        android:icon="@drawable/ic_launcher"
        android:label="@string/status_Update" >
    </activity>
    <activity
        android:name=".TimelineActivity"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".UpdaterService" >
    </service>
    <service
        android:name=".RefreshService"
        android:permission="com.example.yamba.permission.REFRESH" >
        <intent-filter>
            <action android:name="com.example.yamba.RefreshService" />
        </intent-filter>
    </service>

    <activity
        android:name=".PrefsActivity"
        android:label="@string/Preferences" >
    </activity>

    <receiver android:name=".BootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="com.example.yamba.REFRESH_ALARM" />
        </intent-filter>
    </receiver>
</application>

package com.example.yamba;

import java.util.List;

import winterwell.jtwitter.Twitter.Status;
import winterwell.jtwitter.TwitterException;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class RefreshService extends IntentService {
    static final String TAG = "RefreshService";

    public RefreshService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
            ((YambaApp) getApplication()).pullAndInsert();
        Log.d(TAG, "onHandleIntent");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "OnCreated");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Log.d(TAG, "OnDestroy");
    }

}



    public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    Intent intentUpdater = new Intent(this, UpdaterService.class);
    Intent intentRefresh = new Intent(this, RefreshService.class);
    Intent intentPrefs = new Intent(this, PrefsActivity.class);
    Intent intentTimeline = new Intent(this, StatusActivity.class);

    switch (item.getItemId()) {
    case R.id.item_start_service:
        startService(intentUpdater);
        return true;
    case R.id.item_stop_service:
        stopService(intentUpdater);
        return true;
    case R.id.item_refresh:
        startService(intentRefresh);
        return true;
    case R.id.item_prefs:
        startActivity(intentPrefs);
        return true;
    case R.id.item_status_update:
        startActivity(intentTimeline);
    default:
        return false;

    }

onOptionsItemSelected 用于调用刷新服务。

真的很感激帮助。谢谢!!

【问题讨论】:

  • 请发布整个堆栈跟踪,以及您尝试安排启动此服务的代码。
  • CommonsWare 我刚刚从 Logcat 添加了堆栈跟踪。
  • 查看@jvra 的答案——您必须拥有&lt;permission&gt; 元素才能获得任何自定义权限。

标签: android


【解决方案1】:

您需要在清单中声明permission

    <permission 
        android:name="com.example.yamba.permission.REFRESH"        
        android:protectionLevel="signature"
    />

【讨论】:

  • 我在使用 之前已经尝试过了。在清单文件上使用 时仍然会引发相同的错误。
  • 然后你可以检查你的服务类中是否有默认构造函数
  • jvra 问题已解决。我错过了在清单中使用 标记,因此出现了错误。不过谢谢你的帮助。!!
【解决方案2】:

您还需要在清单中声明此刷新权限。 这是一个如何声明权限的示例代码

<permission
    android:name="A_PERMISSION"
    android:description="@string/broadcast_permission_desc"
    android:label="@string/broadcast_permission_label"
    android:permissionGroup="@string/broadcast_permission_group"
    android:protectionLevel="signature" />

参考见Page

【讨论】:

  • 我在使用 之前已经尝试过了。在清单文件上使用 时仍然会引发相同的错误。
  • 老兄,您需要同时使用它们,用户权限和权限。不只是任何一个!
  • 我总是忘记在清单中声明它们。感谢 Ishan Khanna 的帮助!
猜你喜欢
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2012-05-06
  • 2019-11-21
相关资源
最近更新 更多