I have arranged for my app to start when a file with the correct extension is chosen in a file manager. I have this in my manifest:
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:host="*" />
<data android:mimeType="application/vnd.google-earth.kmz" />
</intent-filter>
and in OnCreate() I call this method, to tell the app what to do with the chosen file:
private void associateFile()
{
Intent intent = getIntent();
if (intent.getAction().equals(Intent.ACTION_MAIN))
{
// this is a normal start, not a file manager start
return;
}
Uri uri = intent.getData();
if (uri == null)
{
return;
}
String filePath = FileLoader.LoadFile(this, uri);
processFile(filePath);
}
and it all works fine....until I use the file manager method to start when the app is already running. This causes another copy to start, giving disastrous results with background tasks.
So how to I catch the fact that the app is already running, and just gracefully exit the second attempt at opening, with perhaps an appropriate message to the user? I have tried
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.AppTask> taskInfo = am.getAppTasks();
to see if the app is already running, but the count of taskInfo
is always one, not two as I would expect for the second attempt at opening.
question from:
https://stackoverflow.com/questions/65869444/android-associating-an-app-with-a-file-extension-causes-a-new-copy-to-start 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…