簡體   English   中英

不同類別的意圖定義

[英]Intent definition for different class

我有一個Util類,其中具有用於打開文件的openFile()方法。 我從另一個類(Browser.class)調用此方法,然后嘗試向名為ImageViewer.class的第三類啟動特定的意圖。 我的問題是,我應該如何在Util類中定義可以由任何其他類啟動的意圖? 到目前為止,這是我的定義。

public static void openFile(final Context context, final File target) {
    final String mime = MimeTypes.getMimeType(target);
    final Intent i = new Intent(Intent.ACTION_VIEW);
    final boolean defaultOpen = true;
    if (defaultOpen) {
        if(mime.startsWith("image/")){
            Intent i1 = new Intent(String.valueOf(ImageViewer.class));
            i1.setDataAndType(Uri.fromFile(target), mime);
            context.startActivity(i1);
        }
    }

這是我的日志:

Process: com.tproductions.Openit, PID: 5796
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=class com.tproductions.Openit.ImageViewer dat=file:///storage/emulated/0/DCIM/100ANDRO/DSC_0001.JPG typ=image/jpeg }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
        at android.app.Activity.startActivityForResult(Activity.java:3424)
        at android.app.Activity.startActivityForResult(Activity.java:3385)
        at android.app.Activity.startActivity(Activity.java:3627)
        at android.app.Activity.startActivity(Activity.java:3595)
        at com.tproductions.Openit.utils.SimpleUtils.openFile(SimpleUtils.java:318)
        at com.tproductions.Openit.Browser.listItemAction(Browser.java:446)
        at com.tproductions.Openit.Browser.access$300(Browser.java:46)
        at com.tproductions.Openit.Browser$1.onItemClick(Browser.java:208)

從非Activity類創建意圖,使用context作為構造函數參數

Intent i = new Intent(context, ImageViewer.class);

由於這是非活動類,因此我建議您執行以下操作:

 private Context mContext;

 public utilClassName(Context context) { //pass in the context from the 
                                         //Activity class you'll be using the object
    mContext = context;
 }

接着:

Intent i1 = new Intent(context, ImageViewer.class).
                        setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM