简体   繁体   中英

Android app that checks if there is an installed app to read file of different types(like pdf, docx,pptx,txt,rtf) etc

This code gives the output saying .doc, .tyyt, .txt reader absent and PDF reader is present.

But actually in phone all the readers (pdf, txt, doc) are present and obviously there is no app to handle .tyyt format.

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);
}
}

Here is the layout file

<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>

Here is my manifest file.

 <?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>

An implicit Intent without an action string will match little to nothing. If you are trying to view these files, please use an ACTION_VIEW Intent . The only one on which you are using ACTION_VIEW is the PDF one, which is why that one works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM