在onClick方法中启动服务时应用程序崩溃

时间:2013-11-15 07:19:39

标签: android service permission-denied wifi-direct

我是android的新手,我遇到了一个奇怪的问题......

我正在尝试修改Google提供的WifiDirectDemo,更具体地说,该演示为我们提供了选择我想要传输的图像的自由,但我只是想让它传输特定的图像。原始代码如下:

public class DeviceDetailFragment extends Fragment implements ConnectionInfoListener {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mContentView = inflater.inflate(R.layout.device_detail, null);

    mContentView.findViewById(R.id.btn_start_client).setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // Allow user to pick an image from Gallery or other
                    // registered apps                  
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
                }
            });
    return mContentView;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // User has picked an image. Transfer it to group owner i.e peer using
    // FileTransferService.
    Uri uri = data.getData();
    TextView statusText = (TextView) mContentView.findViewById(R.id.status_text);
    statusText.setText("Sending: " + uri);
    Log.d(WiFiDirectActivity.TAG, "Intent----------- " + uri);
    Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
    serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
    serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uri.toString());
    serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS,
            info.groupOwnerAddress.getHostAddress());
    serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
    getActivity().startService(serviceIntent);
}

它完全正常。但是在我将onActivityResult方法的代码移动到onClick方法之后,我在运行时遇到了一些错误。这是我的代码。

@Override
public void onClick(View v) {
                    // Allow user to pick an image from Gallery or other
                    // registered apps               
    String uristring = "content://com.android.providers.media.documents/document/image%3A25";
    Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
    serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
    serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uristring);
    serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS,
            info.groupOwnerAddress.getHostAddress());
    serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
    getActivity().startService(serviceIntent);
}

错误代码如下:

11-15 00:11:16.397: W/dalvikvm(18595): threadid=11: thread exiting with uncaught exception (group=0x41553b90)
11-15 00:11:16.397: E/AndroidRuntime(18595): FATAL EXCEPTION: IntentService[FileTransferService]
11-15 00:11:16.397: E/AndroidRuntime(18595): Process: com.example.android.wifidirect, PID: 18595
11-15 00:11:16.397: E/AndroidRuntime(18595): java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{42a042a8 18595:com.example.android.wifidirect/u0a77} (pid=18595, uid=10077) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.Parcel.readException(Parcel.java:1461)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.Parcel.readException(Parcel.java:1415)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2848)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.app.ActivityThread.acquireProvider(ActivityThread.java:4396)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2207)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1047)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:904)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.content.ContentResolver.openInputStream(ContentResolver.java:629)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at com.example.android.wifidirect.FileTransferService.onHandleIntent(FileTransferService.java:63)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.Looper.loop(Looper.java:137)
11-15 00:11:16.397: E/AndroidRuntime(18595):    at android.os.HandlerThread.run(HandlerThread.java:61)

有人可以提供帮助吗?

更新

我向清单添加了权限,以下是清单的样子:

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

<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

<!-- Google Play filtering --> 
<uses-feature android:name="android.hardware.wifi.direct" android:required="true"/>


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo">
    <activity
        android:name=".WiFiDirectActivity"
        android:label="@string/app_name" android:launchMode="singleTask">
        <intent-filter>
            <action
                android:name="android.intent.action.MAIN" />
            <category
                android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- Used for transferring files  after a successful connection -->
    <service android:enabled="true" android:name=".FileTransferService" />

</application>

该应用程序仍然崩溃并显示相同的错误信息。

但是当我运行以下代码时,不会发生错误。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

   mContentView = inflater.inflate(R.layout.device_detail, null);
   mContentView.findViewById(R.id.btn_start_client).setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // Allow user to pick an image from Gallery or other
                    // registered apps

                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
                }
            });

    return mContentView;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    // User has picked an image. Transfer it to group owner i.e peer using

    String uristring = "content://com.android.providers.media.documents/document/image%3A25";

    Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
    serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
    serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uristring);
    serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS,
            info.groupOwnerAddress.getHostAddress());
    serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
    getActivity().startService(serviceIntent);
}

因此,当我将onActivityResult的行序列移动到onClick时会发生错误。我只是不知道情况是什么,因为实际上我已经向清单添加了MANAGE_DOCUMENT权限,并且它一直在寻求许可。如果没有manifest中的权限,如果我将代码放回onActivityResult,我仍然可以访问图像文件。有谁知道为什么?非常感谢你!

3 个答案:

答案 0 :(得分:0)

在清单中声明权限

   <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

答案 1 :(得分:0)

我认为您在清单文件中缺少某些权限 在清单文件中添加

阅读此文档https://developer.android.com/guide/topics/providers/document-provider.html

 <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

答案 2 :(得分:0)

您需要在清单中添加以下权限

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>