Android应用程序检查是否有已安装的应用程序来读取不同类型的文件(如pdf,docx,pptx,txt,rtf)等

时间:2015-05-03 17:12:56

标签: android android-intent

此代码提供输出,说明缺少.doc,.tyyt,.txt读取器和PDF阅读器。

但实际上在手机中所有的读者(pdf,txt,doc)都存在,显然没有应用程序可以处理.tyyt格式。

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import java.io.File;
import android.content.ContentProvider;
public class MainActivity extends ActionBarActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1=(Button)findViewById(R.id.button1);
    final EditText t1=(EditText)findViewById(R.id.textView1);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PackageManager pm3=getPackageManager();
            String msg=" ",type;
            PackageManager pm2=getPackageManager();
            MimeTypeMap mime2= MimeTypeMap.getSingleton();
            String type2=mime2.getMimeTypeFromExtension(".doc");
            Intent intent2=new Intent();
            intent2.setType(type2);
            if(pm2.queryIntentActivities(intent2,PackageManager.MATCH_DEFAULT_ONLY).size()>0)
            {
                msg+="doc reader present";
                t1.setText(msg);

            }
            else
            {
                msg+="doc reader absent";
                t1.setText(msg);
            }


            MimeTypeMap mime3= MimeTypeMap.getSingleton();
            // I am just trying to check if it works by setting an  unkonwn file type,obiviously it should not be present
            String type3=mime3.getExtensionFromMimeType(".tyyt");
            Intent intent3=new Intent();
            intent3.setType(type3);
            if(pm3.queryBroadcastReceivers(intent3,PackageManager.MATCH_DEFAULT_ONLY).size()>0)
            {
                msg+="random reader present";
                t1.setText(msg);

            }
            else
            {
                msg+="random reader absent";
                t1.setText(msg);
            }



            PackageManager pm1=getPackageManager();
            MimeTypeMap mime1= MimeTypeMap.getSingleton();
            String  type1=mime1.getMimeTypeFromExtension(".txt");
            Intent intent1=new Intent();
            intent1.setType(type1);
            if(pm1.queryIntentActivities(intent1,PackageManager.MATCH_DEFAULT_ONLY).size()>0)
            {
                msg+="txt reader present";
                t1.setText(msg);

            }
            else {
                msg+="txt reader absent";
                t1.setText(msg);
            }


            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            PackageManager pm=getPackageManager();
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            type=mime.getMimeTypeFromExtension(".pdf");
            intent.setType(type);
            if(pm.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY).size()>0)
            {
               msg+="pdf reader present";
                t1.setText(msg);

            }
            else
             {
                msg+="pdf reader absent";
                t1.setText(msg);
            }







        }
    });


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这是布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"          android:layout_width="match_parent"
android:layout_height="match_parent"   android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"   tools:context=".MainActivity">

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button1"
    android:layout_marginTop="83dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Receive"
    android:id="@+id/textView1"
    android:layout_below="@+id/button1"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignStart="@+id/button1" />
    </RelativeLayout>

这是我的清单文件。

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shiva.com.opendefaultapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    android:debuggable="true"
</application>

1 个答案:

答案 0 :(得分:1)

没有动作字符串的隐式Intent几乎没有匹配。如果您要查看这些文件,请使用ACTION_VIEW Intent。您使用ACTION_VIEW的唯一一个是PDF,这就是为什么有效的原因。