如何在完整的操作选项中包含我的应用程序

时间:2012-06-29 02:20:18

标签: android

当用户使用Android下载文件时,我希望启动自定义活动以打开该文件。例如,我的自定义活动应该在文件启动时显示在“使用完整操作”警告框中。

有没有例子可以看看这是怎么做的?

1 个答案:

答案 0 :(得分:1)

如果我是对的,这将是你在清单中想要的东西:

<activity
     android:name=".NameOfYourActivity"
     android:label="@string/app_name" >
            <intent-filter>
                  <action android:name="android.intent.action.VIEW" />
                  <category android:name="android.intent.category.DEFAULT" />
                  <data android:mimeType="text/plain" />
            </intent-filter>
</activity>

有关详细信息,请阅读开发人员网站上的Intents and Intent Filters

此外,这是一个可用于显示文件的活动示例。

public class MIMEsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    //Get the intent that has the information about the file.
    Intent sender = getIntent();

    //In this example I'm simply displaying the file's contents
    //in a TextView.
    TextView view = (TextView) findViewById(R.id.textview);

    //Check to see if there was an intent sent.
    if(sender != null) {

        //Get the file.
        File file = new File(sender.getData().getPath());

        /*
            DO STUFF HERE WITH THE FILE
            I load the text of the file and send it
            to the TextView.
        */
        StringBuilder text = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        }
        catch (IOException e) {
            //You'll need to add proper error handling here
        }

        view.setText("PATH: " + sender.getData().getPath() + "\n\n" + text);
        //Done doing stuff.
    }
    //If an intent was not sent, do something else.
    else {
        view.setText("You did not get a file!");
    }

}

}