问题描述
我有一个文本阅读器应用程序,旨在接受意图从Android系统中,当我点击一个文本文件,将其打开。但我的应用程序是没有弹出的系统就行了。下面是我的codeS:
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>
我的扩展的BroadcastReceiver
My extended BroadcastReceiver
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);
}
}
}
谢谢!
推荐答案
您需要将您的应用程序与文件扩展名关联。要做到这一点,添加在意图过滤器这两个线u'r好走
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>
&lt;数据机器人:计划=文件/&GT;
=>这个定义文件必须是本地的,而不是从HTTP或者
<data android:scheme="file" />
=> this define that the file must be local, not from http or else
&lt;数据机器人:MIMETYPE =* / */&GT;
=>匹配任何MIME类型
<data android:mimeType="*/*" />
=> match any mime type
&lt;数据机器人:pathPattern =*。\\ TXT/&GT;
=>这就是你指定要匹配的内容扩展
<data android:pathPattern=".*\\.txt" />
=> this is where you specify what extension you want to match
希望这有助于
这篇关于为什么不是我的应用程序打开txt文件的清单上的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!