为什么我的应用程序不在打开txt文件的应用程序列表中? [英] Why isn't my app on the list of apps to open txt file?
问题描述
我有一个文本阅读器应用程序,旨在当我单击文本文件打开它时从 Android 系统接收意图.但是我的应用程序不在系统弹出的列表中.以下是我的代码:
I have an text reader app that is designed to receive intent from Android system when I click on a text file to open it. But my app isn't on the list popped up by the system. Below are my codes:
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastreceivertest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".BroadcastReceiverTest1Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_VIEW" />
<action android:name="android.intent.action.ACTION_EDIT" />
<action android:name="android.intent.action.ACTION_PICK" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\.txt" />
<data android:host="*" />
</intent-filter>
</receiver>
</application>
</manifest>
我的扩展广播接收器
public final class MyBroadcastReceiver extends BroadcastReceiver {
private String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, BroadcastReceiverTest1Activity.class);
i.putExtra("URI", intent.getData());
context.startActivity(i);
Log.d(TAG, "Leaving onReceived...");
}
}
我的活动被广播接收者打开
My activity to be opened by the broadcast receiver
public class BroadcastReceiverTest1Activity extends Activity {
private String uri ="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent intent = getIntent();
final String action = intent.getAction();
if(Intent.ACTION_VIEW.equals(action)){
uri = intent.getStringExtra("URI");
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(uri);
}
}
}
谢谢!
推荐答案
您需要将您的应用与文件扩展名相关联.为此,请在意图过滤器中添加这两行,然后就可以了
You need to associate your app with file extension. To do so, add these two line within intent filter and u'r good to go
<data android:scheme="file" />
<data android:mimeType="*/*"/>
<data android:pathPattern=".*\.pdf" />
你的清单看起来像这样
<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=".*\.txt" />
</intent-filter>
</activity>
<data android:scheme="file"/>
=> 这定义了文件必须是本地的,不能来自 http 或其他
<data android:scheme="file" />
=> this define that the file must be local, not from http or else
<data android:mimeType="*/*"/>
=> 匹配任何 mime 类型
<data android:mimeType="*/*" />
=> match any mime type
<data android:pathPattern=".*\.txt"/>
=> 这是您指定要匹配的扩展名的地方
<data android:pathPattern=".*\.txt" />
=> this is where you specify what extension you want to match
希望对您有所帮助
这篇关于为什么我的应用程序不在打开txt文件的应用程序列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!