我需要我的 Android 应用程序出现在Open With
SQLite 文件的对话框中。
就像您安装新的网络浏览器一样,它会出现在Open With
html 文件的对话框中。
我怎样才能做到这一点?
我在俄语 StackOverflow 中找到了这个答案: https ://ru.stackoverflow.com/a/420927/180697
<activity name="com.your.activity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.sqlite" />
</intent-filter>
这是您需要添加到 Activity 类的内容:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent = getIntent();
final String action = intent.getAction();
if(Intent.ACTION_VIEW.equals(action)){
Uri uri = intent.getData();
new File(uri.getPath()); //дальше делаем все, что надо с файлом
} else {
Log.d(TAG, "intent was something else: "+action);
}
}
所以我只需要了解在活动中写什么!))谢谢!
要出现在“打开方式”对话框中,您的 Android 应用程序必须在其清单中声明它处理特定的意图,然后在意图中指定文件的 mime 类型。例如 :
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/x-sqlite" />
</intent-filter>
请注意,SQLite 的 mime 类型可能无法识别,因为我认为这还不是标准。您可能想要使用 application/octet-stream 代替,然后在您自己的代码中,仔细检查所提供的文件实际上是一个有效的 SQLite 文件(无论如何您都应该这样做)。