0

好的,我是 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 用于调用刷新服务。

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

4

2 回答 2

2

你需要permission在你的清单中声明

    <permission 
        android:name="com.example.yamba.permission.REFRESH"        
        android:protectionLevel="signature"
    />
于 2013-06-14T17:40:58.393 回答
1

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

<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" />

参考请参阅此页面

于 2013-06-14T17:41:09.807 回答