0

我的应用程序允许其他可以共享文件的应用程序在共享菜单上选择它,打开应用程序并将文件“传输”到我的应用程序。

我的问题是当他们从共享菜单中选择我的应用程序时(例如,在图库中,选择几张照片后,他们选择与我的应用程序共享)登录活动在图库应用程序中打开

请注意,我已经尝试过此过程适用于旧设备并且它有效,但不适用于较新的设备。

这是我的 manifest.xml

<application
    android:name="com.Application"
    android:allowBackup="true"
    android:fullBackupContent="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:mimeType="*/*"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustPan"/>
    <activity
        android:name=".login.LoginActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:windowSoftInputMode="adjustResize|stateVisible">
        <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.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
    </activity>
</application>

我有这个应用程序的两个活动,登录和主,如果登录成功,主将被加载。

我尝试了不同类型的启动器模式设置,但没有什么能阻止在图库中创建实例

这是来自我的登录活动

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);
    receivedIntent = getIntent();
    //set orientation
    setRequestedOrientation(isPhone() ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent != null && intent.getAction() != null && (intent.getAction().equals(Intent.ACTION_SEND) || intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE))){
        finish();
        startActivity(intent);
    }
}

问题是,在旧设备上,onNewIntent 方法按预期调用,但在新设备上它只是忽略启动器模式并创建新实例并运行 onCreate()

关于如何让应用程序在从共享菜单调用时“强制”启动自己的任何想法?

当前测试设备 - 三星移动 Android 8.0,API 26 - 不工作三星平板电脑 Android 7.1.1,API 25 - 按预期工作

4

2 回答 2

0

我对设备上的其他应用程序进行了一些研究,发现仅在从应用程序“图库”共享图像时才会出现此问题,否则代码按预期工作,因此我认为这可能是图库本身的问题,或者是预期的行为图库应用程序。

于 2018-10-25T06:10:30.027 回答
0

您的所有问题都与权限有关,kitkat(4.4.2)以下的旧设备不需要文件权限,但在新设备中需要访问文件数据的权限(即画廊和其他)。您必须以编程方式授予权限,

if (Build.VERSION.SDK_INT > 19) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            arrPerm.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (!arrPerm.isEmpty()) {
            String[] permissions = new String[arrPerm.size()];
            permissions = arrPerm.toArray(permissions);
            ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
        } 
    }

授予权限后,您可以进行操作:

if (Build.VERSION.SDK_INT > 19) {
                    if (isPermissionGranted()) {
                        //Do your Task of gallery
                    } 
                }

或者其他方式,您可以直接使用库: https ://github.com/nabinbhandari/Android-Permissions

于 2018-10-25T05:45:33.703 回答