11

当我将启动模式设置为“singleInstance”时,我在 MYApp 中打开电子邮件时遇到问题。

我附上了从电子邮件附件中读取文件名并将其显示在屏幕上的示例 Android 项目。在 onCreate 的情况下工作正常,但当应用程序启动模式为 singleInstance 时会在 onNewIntent 中引发错误。

启动模式.java

package your.namespace.launchmode;


public class LaunchModeActivity extends Activity {
    private static final int OPEN_ACT = 2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String name = getAttachmetName(getIntent());
    if(null != name)
    {
        TextView textv = (TextView) findViewById(R.id.attachmentnm);
        textv.setText(name);
    }
}

@Override
protected void onNewIntent(Intent savedInstanceState)
{
    super.onNewIntent(savedInstanceState);
    String name = getAttachmetName(savedInstanceState);
    if(null != name)
    {
        TextView textv = (TextView) findViewById(R.id.attachmentnm);
        textv.setText(name);
    }
}


private String getAttachmetName(Intent intent) {
    final Uri documentUri = intent.getData();
    if(null != documentUri){
    final String uriString = documentUri.toString();
    String documentFilename = null;


    final int mailIndexPos = uriString.lastIndexOf("/attachments");
    if (mailIndexPos != -1) {
        final Uri curi = documentUri;
        final String [] projection = new String[] {OpenableColumns.DISPLAY_NAME};
        final Cursor cursor = getApplicationContext().getContentResolver().query(curi, projection, null, null, null);
        if (cursor != null) {
            final int attIdx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            if (attIdx != -1) {
                cursor.moveToFirst();
                documentFilename = cursor.getString(attIdx);                
            }
            cursor.close();
        }
    }
    return documentFilename;
    }
    return null;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if((resultCode == RESULT_OK) && (requestCode == OPEN_ACT))
    {
        Log.d("LaunchMode", "Second activity returned");
    }
}

}

AndroidManifest

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.namespace.launchmode"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="com.google.android.gm.permission.READ_GMAIL"/>
    <uses-permission android:name="com.google.android.gm.permission.WRITE_GMAIL"/>
    <uses-permission android:name="com.google.android.providers.gmail.permission.READ_GMAIL"/>
    <uses-permission android:name="com.google.android.providers.gmail.permission.WRITE_GMAIL"/>
    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:launchMode="singleInstance"
            android:name=".LaunchModeActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter >
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <!-- docx -->
                <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
                <!-- xlsx -->
                <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
                <!-- pptx -->
                <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
                <data android:mimeType="application/vnd.ms-excel" />
                <data android:mimeType="application/msword" />
                <data android:mimeType="application/vnd.ms-powerpoint" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>
</manifest>

重现步骤 1) 在设备上安装 apk。2)进入设备上的gmail原生应用,打开任意附件(办公文档)查看。3) 选择 LaunchMode 应用程序以完成操作。4)LaunchMode 应用程序将在屏幕上显示文件名。

这第一次工作正常(onCreate 流程)但是当这个应用程序在后台切换时,我再次尝试 2,3,4 步骤.. 应用程序崩溃并出现错误

E/DatabaseUtils(30615): java.lang.SecurityException: Permission Denial: reading com.google.android.gm.provider.MailProvider uri content://gmail-ls/qoconnect@gmail.com/messages/5/attachments/0.2/BEST/false from pid=32657, uid=10058 requires com.google.android.gm.permission.READ_GMAIL
E/DatabaseUtils(30615):     at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:309)
E/DatabaseUtils(30615):     at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:178)
E/DatabaseUtils(30615):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
E/DatabaseUtils(30615):     at android.os.Binder.execTransact(Binder.java:339)
E/DatabaseUtils(30615):     at dalvik.system.NativeStart.run(Native Method)
D/AndroidRuntime(32657): Shutting down VM

我需要解决这个问题,我需要有单个应用程序实例,并且也应该获取电子邮件附件名称。如果我在这里遗漏了什么,请告诉我。

我的问题是为什么它在 onCreate 的流程中工作,而在 onNewIntent 的情况下它不会工作

注意:1)适用于 2.x 手机 2)适用于单顶启动模式。3) Gmail 应用程序的一些更新。链接在这里:

4

2 回答 2

5

当您收到意图并且未使用您请求的权限(READ_GMAILWRITE_GMAIL)时,您可能获得了读取文件名的 URI 权限。URI 权限仅在您的应用程序finish()es 之前有效,因此您尝试恢复时不会拥有它。

这与你的经验是一致的——当意图是新鲜的,但不是旧的时,它会起作用。我认为这WRITE_GMAIL是一个签名许可,我猜READ_GMAIL也是如此。在这种情况下,您无能为力。 READ_ATTACHMENT可能是您请求的更合适的权限。

有关 URI 权限的更多信息:http: //developer.android.com/guide/topics/security/permissions.html#uri

尝试uses-permission从清单中删除标签,看看您是否有相同的经历。您还可以尝试intent通过检查其标志来检查收到它的时间。

checkCallingOrSelfUriPermission(documentUri , Intent.FLAG_GRANT_READ_URI_PERMISSION)

如果返回 0,则表示您一直在使用 URI 权限。

于 2012-07-30T15:02:11.063 回答
0

就像 skoke 所说,如果授予您许可的意图不是新鲜的,您将无法再从 GMail 中读取,即它必须是原始活动意图。如果您的意图是onNewIntent从那时起,它可能不会成功。

我的解决方案并不漂亮,但似乎有效。在我的onResume我调用了一个自定义函数来查看我是否可以访问 Gmail 内容。如果没有,我向用户显示一条消息并要求他们关闭应用程序并重试。嘿,至少它不会崩溃。

于 2014-11-27T01:28:37.020 回答