GVKun编程网logo

使用 Javascript 和 Android Intent 启动 TOMTOM 导航(js启动app)

3

如果您对使用Javascript和AndroidIntent启动TOMTOM导航感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于使用Javascript和AndroidInt

如果您对使用 Javascript 和 Android Intent 启动 TOMTOM 导航感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于使用 Javascript 和 Android Intent 启动 TOMTOM 导航的详细内容,我们还将为您解答js启动app的相关问题,并且为您提供关于Android Intent and Intent Filter、android – Activity.finishAffinity()vs Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK、android – Intent.FLAG_ACTIVITY_CLEAR_TOP和Intent.FLAG_ACTIVITY_CLEAR_TOP之间有什么区别吗? Intent.FLAG_ACTIVITY_SINGLE_TOP、android – intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,bmp)图像偏离中心的有价值信息。

本文目录一览:

使用 Javascript 和 Android Intent 启动 TOMTOM 导航(js启动app)

使用 Javascript 和 Android Intent 启动 TOMTOM 导航(js启动app)

如何解决使用 Javascript 和 Android Intent 启动 TOMTOM 导航

我有一个 Web 应用程序,需要在设备的本机地图/导航应用程序中打开目的地。在大多数 Android 设备上,这是谷歌地图,我可以简单地打开一个链接,该链接将打开谷歌地图,其中包含到达目的地的路线(纬度、经度坐标)。对于 TomTom Pro 8375,似乎无法从浏览器打开导航器应用程序中的目的地。

我们已经尝试了以下方法:

    1. tomtomgo://x-callback-url/navigate?destination=52.371183,4.892504
    1. tomtomhome://geo:action=navigateto&lat=mylat&long=mylon&name=myname

是否有任何已知的解决方法来实现这一目标? 我收到了来自支持人员的回复,但这仅适用于 kotlin 或 java。

Uri gmmIntentUri = Uri.parse("google.navigation:q=52.0000,12.0000");

Intent mapIntent = new Intent(Intent.ACTION_VIEW,gmmIntentUri);

startActivity(mapIntent)

有什么方法可以让我在 Javascript 中完成这项工作吗?

我真的很感激任何建议!

谢谢!

解决方法

评论太长了

对 webapps 不太熟悉,但是我们有 3 个带有 Webfleet 的应用程序。遗憾的是,我们使用 Java 而不是 Javascript。我们使用的意图是

  1. String uri = "geo:0,0?q=" + <destination>;
  2. startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri)));

对您没有太大帮助,但我们必须同时支持手机和 Pro 平板电脑。

下面的格式是 JavaScript 的标准吗?

  1. intent:
  2. HOST/URI-path // Optional host
  3. #Intent;
  4. package=[string];
  5. action=[string];
  6. category=[string];
  7. component=[string];
  8. scheme=[string];
  9. end;

我会给你一个粗略的估计:

  1. intent://#Intent;action=android.intent.action.PICK;type=text/plain;S.android.intent.extra.TEXT=geo:0,0?q=52.0000,12.0000;end

我还要添加一个名为NavPadNavAppActivity的Webfleets版地图应用,可以尝试直接定位吗?

Android Intent and Intent Filter

Android Intent and Intent Filter

Intents and Intent Filters

 

三种应用程序基本组件 ——activity, service 和 broadcast receiver—— 是使用称为 intent 的消息来激活的。Intent 消息传递是一种组件间运行时绑定的机制. intent 是 Intent 对象,它包含了需要做的操作的描述,或者,对于广播来说,包含了正在通知的消息内容。对于向这三种组件发送 intent 有不同的机制:

  • 使用 Context.startActivity() 或 Activity.startActivityForResult (), 传入一个 intent 来启动一个 activity. 使用 Activity.setResult(), 传入一个 intent 来从 activity 中返回结果.
  • 将 intent 对象传给 Context.startService() 来启动一个 service 或者传消息给一个运行的 service. 将 intent 对象传给 Context.bindService() 来绑定一个 service.
  • 将 intent 对象传给 Context.sendBroadcast()Context.sendOrderedBroadcast(), 或者 Context.sendStickyBroadcast() 等广播方法,则它们被传给 broadcast receiver.

 

在上述三种情况下,android 系统会自己找到合适的 activity, service, 或者  broadcast receivers 来响应 intent. 三者的 intent 相互独立互不干扰.

 

Intent Objects Intent 对象

 

一个 intent 对象包含了接受该 intent 的组件的信息 (例如需要的动作和该动作需要的数据) 和 android 系统所需要的信息 (例如该组件的类别,以及如何启动它). 具体的说:

组件名称
为一个 ComponentName 对象。它是目标组件的完整名 (例如 " com.example.project.app.FreneticActivity") 和应用程序 manifest 文件设定的包名 (例如" com.example.project") 的组合。前者的包名部分和后者不一定一样.

 

组件名称是可选的。如果设定了的话,Intent 对象会被传给指定的类的一个实例。如果不设定,则 android 使用其它信息来定位合适的目标.

 

组件名称是使用 setComponent()setClass(), 或  setClassName() 来设定,使用 getComponent() 来获取.

Action
一个字符串,为请求的动作命名,或者,对于 broadcast intent, 发生的并且正在被报告的动作。例如:
常量 目标组件
动作
ACTION_CALL activity 发起一个电话呼叫.
ACTION_EDIT activity 显示数据给用户来编辑.
ACTION_MAIN activity 将该 activity 作为一个 task 的第一个 activity 启动,不传入参数也不期望返回值.
ACTION_SYNC activity 将设备上的数据和一个服务器同步.
ACTION_BATTERY_LOW broadcast receiver 发出电量不足的警告.
ACTION_HEADSET_PLUG broadcast receiver 一个耳机正被插入或者拔出.
ACTION_SCREEN_ON broadcast receiver 屏幕被点亮.
ACTION_TIMEZONE_CHANGED broadcast receiver 时区设置改变.

 

你也可以定义自己的 action 字符串用来启动你的应用程序。自定义的 action 应该包含应用程序的包名。例如 "com.example.project.SHOW_COLOR".

 

action 很大程度上决定了 intent 的另外部分的结构,就像一个方法名决定了它接受的参数和返回值一样。因此,最好给 action 一个最能反映其作用的名字.

 

一个 intent 对象中的 action 是使用 getAction () 和 setAction () 来读写的.

Data
需要操作的数据的 URI 和它的 MIME (多用途互联网邮件扩展,Multipurpose Internet Mail Extensions) 类型。例如,如果 action 为 ACTION_EDIT, 那么 Data 将包含待编辑的数据 URI. 如果 action 为 ACTION_CALL, Data 将为 tel: 电话号码的 URI. 如果 action 为 ACTION_VIEW, 则 Data 为 http: 网络地址的 URI.

 

当将一个 intent 和一个组件相匹配时,除了 URI 外数据类型也很重要。例如,一个显示图片的程序不应该用来处理声音文件.

 

数据类型常常可以从 URI 推断,特别是 content:URI, 它表示该数据属于一个 content provider. 但数据类型也可以被 intent 对象显示声明. setData () 方法设置 URI, 而 setType () 方法指定 MIME 类型,setDataAndType () 设置数据 URI 和 MIME 类型。它们可以使用 getData () 和 getType () 来读取.

Category
一个字符串,包含了关于处理该 intent 的组件的种类的信息。一个 intent 对象可以有任意个 category. intent 类定义了许多 category 常数,例如:
常量 含义
CATEGORY_BROWSABLE 目标 activity 可以使用浏览器来显示 - 例如图片或电子邮件消息.
CATEGORY_GADGET 该 activity 可以被包含在另外一个装载小工具的 activity 中.
CATEGORY_HOME 该 activity 显示主屏幕,也就是用户按下 Home 键看到的界面.
CATEGORY_LAUNCHER 该 activity 可以作为一个任务的第一个 activity, 并且列在应用程序启动器中.
CATEGORY_PREFERENCE 该 activity 是一个选项面板.

 

 

addCategory () 方法为一个 intent 对象增加一个 category, removeCategory 删除一个 category, getCategories () 获取 intent 所有的 category.

Extras
为键 - 值对形式的附加信息。例如 ACTION_TIMEZONE_CHANGED 的 intent 有一个 "time-zone" 附加信息来指明新的时区,而 ACTION_HEADSET_PLUG 有一个 "state" 附加信息来指示耳机是被插入还是被拔出.

 

intent 对象有一系列 put...() 和 set...() 方法来设定和获取附加信息。这些方法和 Bundle 对象很像。事实上附加信息可以使用 putExtras () 和 getExtras () 作为 Bundle 来读和写.

Flags

各种标志。很多标志指示 android 系统如何启动一个 activity (例如该 activity 属于哪个任务) 和启动后如何处理它 (例如,它是否属于最近 activity 列表中).

 

android 系统和应用程序使用 intent 对象来送出系统广播和激活系统定义的组件.

Intent Resolution Intent 解析

 

intent 有两种:

  • 显式 intent 使用名字来指定目标组件。由于组件名称一般不会被其它开发者所熟知,这种 intent 一般用于应用程序内部消息 -- 例如一个 activity 启动一个附属的 service 或者另一个 activity.
  • 隐式 intent 不指定目标的名称。一般用于启动其它应用程序的组件.

Android 将显式 intent 发送给指定的类. intent 对象中名字唯一决定接受 intent 的对象.

 

对于隐式 intent, android 系统必须找到最合适的组件来处理它。它比较 intent 的内容和 intent filter.  intent filter 是组件的一个相关结构,表示其接受 intent 的能力. android 系统根据 intent filter 打开可以接受 intent 的组件。如果一个组件没有 intent filter, 那么它只能接受显式 intent. 如果有,则能同时接受二者.

当一个 intent 和 intent filter 比较时,只考虑三个属性: action, data, category.

extra 和 flag 在 intent 解析中没有用.

Intent filters

 

activity, service 和 broadcast receiver 可以有多个 intent filter 来告知系统它们能接受什么样的隐式 intent. intent filter 的名字很形象:它过滤掉不想接受的 intent, 留下想接受的 intent. 显式 intent 无视 intent filter.

一个组件对能做的每件事有单独的 filter. 例如,记事本程序的 NoteEditor activity 有两个 filter -- 一个启动并显示一个特定的记录给用户查看或编辑,另一个启动一个空的记录给用户编辑.

 

Filters and security Filter 和安全

 

一个 intent filter 不一定安全可靠。一个应用程序可以让它的某个组件去接受隐式 intent, 但是它没法防止这个组件接受显示 intent. 其它的程序总是可以使用自定义的数据加上显式的程序名称来调用该组件.

 

一个 intent filter 是 IntentFilter 类的实例,但是它一般不出现在代码中,而是出现在 android Manifest 文件中,以 <intent-filter> 的形式. (有一个例外是 broadcast receiver 的 intent filter 是使用 Context.registerReceiver() 来动态设定的,其 intent filter 也是在代码中创建的.)

一个 filter 有 action, data, category 等字段。一个隐式 intent 为了能被某个 intent filter 接受,必须通过 3 个测试。一个 intent 为了被某个组件接受,则必须通过它所有的 intent filter 中的一个.

 

Action 测试
<intent-filter . . . >
    
<action android:name="com.example.project.SHOW_CURRENT" />
    
<action android:name="com.example.project.SHOW_RECENT" />
    
<action android:name="com.example.project.SHOW_PENDING" />
    . . .
</intent-filter>

 

 

一个 intent 对象只能指定一个 action, 而一个 intent filter 可以指定多个 action. action 列表不能为空,否则它将组织所有的 intent.

 

一个 intent 对象的 action 必须和 intent filter 中的某一个 action 匹配,才能通过.

如果 intent filter 的 action 列表为空,则不通过.

如果 intent 对象不指定 action, 并且 intent filter 的 action 列表不为空,则通过.

Category 测试

<intent-filter . . . >

    <category android:name="android.intent.category.DEFAULT" />
    
<category android:name="android.intent.category.BROWSABLE" />
    . . .
</intent-filter>

 

 

注意前面说到的对于 action 和 category 的常数是在代码中用的,而不是 manifest 文件中用的。例如,CATEGORY_BROWSABLE 常数对应 xml 中的表示为 "android.intent.category.BROWSABLE".

一个 intent 要通过 category 测试,那么该 intent 对象中的每个 category 都必须和 filter 中的某一个匹配.

 

理论上来说,一个 intent 对象如果没有指定 category 的话,它应该能通过任意的 category 测试。有一个例外: android 把所有的传给 startActivity () 的隐式 intent 看做至少有一个 category: "android.intent.category.DEFAULT". 因此,想要接受隐式 intent 的 activity 必须在 intent filter 中加入"android.intent.category.DEFAULT". ("android.intent.action.MAIN"和"android.intent.category.LAUNCHER"的 intent filter 例外。它们不需要"android.intent.category.DEFAULT".)


Data test
<intent-filter . . . >
    
<data android:mimeType="video/mpeg" android:scheme="http" . . . />
    
<data android:mimeType="audio/mpeg" android:scheme="http" . . . />
    . . .
</intent-filter>

 

 

每个 <data> 元素指定了一个 URI 和一个数据类型. URI 每个部分为不同的属性 -- scheme, host, port, path:

scheme://host:port/path

例如,在如下的 URI 中:

content://com.example.project:200/folder/subfolder/etc

 

scheme 为 "content", host 为 "com.example.project", port 为 "200", path 为 "folder/subfolder/etc". host 和 port 一起组成了 URI authority. 如果 host 未指定,则 port 被忽略.

 

这些属性都是可选的,但它们并非相互独立:要使一个 authority 有意义,必须指定一个 scheme. 要使一个 path 有意义,必须指定一个 scheme 和一个 authority.

 

当 intent 对象中的 URI 和 intent filter 中相比较时,它只和 filter 中定义了的部分比较。例如,如果 filter 中之定义了 scheme, 那么所有包含该 scheme 的 URI 的 intent 对象都通过测试。对于 path 来说,可以使用通配符来进行部分匹配.

<data> 元素的 type 属性指定了数据类型。它在 filter 中比在 URI 中更常见. intent 对象和 filter 都可以使用 "*" 通配符作为子类型。例如 "text/*" "audio/*" 表示所有的子类型都匹配.

 

data 测试的规则如下:

  1. 一个不含 uri 也不含数据类型的 intent 对象只通过两者都不包含的 filter.
  2. 一个含 uri 但不含数据类型的 intent 对象 (并且不能从 uri 推断数据类型的) 只能通过这样的 filter: uri 匹配,并且不指定类型。这种情况限于类似 mailto: 和 tel: 这样的不指定实际数据的 uri.
  3. 一个只包含数据类型但不包含 URI 的 intent 只通过这样的 filter: 该 filter 只列出相同的数据类型,并且不指定 uri.
  4. 一个既包含 uri 又包含数据类型的 intent 对象只通过这样的 filter: intent 对象的数据类型和 filter 中的一个类型匹配,intent 对象的 uri 要么和 filter 的 uri 匹配,要么 intent 对象的 uri 为 content: 或者 file:, 并且 filter 不指定 uri.

 

如果一个 intent 可以通过多于一个 activity 或者 service 的 filter, 那么用户可能会被询问需要启动哪一个。如果一个都没有的话,那么会抛出异常.

Common cases 常见情况

 

上述的最后一个规则 (d) 说明了组件通常可以从文件和 content provider 中获取数据。因此,它们的 filter 可以只列出数据类型不列 scheme. 这是个特殊情况。下列 < data > 元素告诉 android 该组件可以从一个 content provider 取得图像数据并显示之:

<data android:mimeType="image/*" />

 

由于大部分可用的数据由 content provider 提供,指定数据类型但不指定 uri 的 filter 是最常见的情况.

 

另外一个常见的配置是 filter 具有一个 scheme 和一个数据类型。例如,下列 <data> 元素告诉 android 该 component 可以从网络获取图像数据并显示之:

<data android:scheme="http" android:type="video/*" />

 

考虑用户点击一个网页时浏览器的动作。它首先试图显示这个数据 (当做一个 html 页来处理). 如果无法显示,则创建一个隐式 intent, 并启动一个可以处理它的 activity. 如果没有这样的 activity, 那么它请求下载管理器来下载该数据。然后它将数据置于一个 content provider 的控制之下,这样有很多 activity (拥有只有数据类型的 filter) 可以处理这些数据.

 

大部分应用程序还有一种方法来单独启动,不需要引用任何特定的数据。这些能启动应用程序的 activity 具有 action 为 "android.intent.action.MAIN"的 filter. 如果它们需要在应用程序启动器中显示,它们必须指定"android.intent.category.LAUNCHER" 的 category.

<intent-filter . . . >
    
<action android:name="code android.intent.action.MAIN" />
    
<category android:name="code android.intent.category.LAUNCHER" />
</intent-filter>

Using intent matching 使用 intent 匹配

 

intent 和 intent filter 相匹配,不仅为了寻找并启动一个目标组件,也是为了寻找设备上组件的信息。例如,android 系统启动了应用程序启动器,该程序位于屏幕的顶层,显示了用户可以启动的程序,这是通过查找设备上所有的 action 为 "android.intent.action.MAIN",category 为"android.intent.category.LAUNCHER"的 intent filter 所在的 activity 实现的。然后它显示了这些 activity 的图标和标题。类似的,它通过寻找"android.intent.category.HOME" 的 filter 来定位主屏幕程序.

 

应用程序可以用相同的方式来使用 intent 匹配. PackageManager 有一组 query...() 方法来寻找接受某个特定 intent 的所有组件,还有一系列 resolve...() 方法来决定响应一个 intent 的最佳组件。例如, queryIntentActivities() 返回一个 activity 列表,这些 activity 可以执行传入的 intent. 类似的还有 queryIntentServices () 和 queryIntentBroadcastReceivers ().

Note Pad Example 例子:记事本

 

记事本示例程序让用户可以浏览一个笔记列表,查看,编辑,删除和增加笔记。这一节关注该程序定义的 intent filter.

 

 

在其 manifest 文件中,记事本程序定义了三个 activity, 每个有至少一个 intent filter. 它还定义了一个 content provider 来管理笔记数据. manifest 文件如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          
package="com.example.android.notepad">
    
<application android:icon="@drawable/app_notes"
                 
android:label="@string/app_name" >

        
<provider android:name="NotePadProvider"
                  
android:authorities="com.google.provider.NotePad" />

        
<activity android:name="NotesList" android:label="@string/title_notes_list">
            
<intent-filter>
                
<action android:name="android.intent.action.MAIN" />
                
<category android:name="android.intent.category.LAUNCHER" />
            
</intent-filter>
            
<intent-filter>
                
<action android:name="android.intent.action.VIEW" />
                
<action android:name="android.intent.action.EDIT" />
                
<action android:name="android.intent.action.PICK" />
                
<category android:name="android.intent.category.DEFAULT" />
                
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
            
</intent-filter>
            
<intent-filter>
                
<action android:name="android.intent.action.GET_CONTENT" />
                
<category android:name="android.intent.category.DEFAULT" />
                
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
            
</intent-filter>
        
</activity>
        
        
<activity android:name="NoteEditor"
                  
android:theme="@android:style/Theme.Light"
                  
android:label="@string/title_note" >
            
<intent-filter android:label="@string/resolve_edit">
                
<action android:name="android.intent.action.VIEW" />
                
<action android:name="android.intent.action.EDIT" />
                
<action android:name="com.android.notepad.action.EDIT_NOTE" />
                
<category android:name="android.intent.category.DEFAULT" />
                
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
            
</intent-filter>
            
<intent-filter>
                
<action android:name="android.intent.action.INSERT" />
                
<category android:name="android.intent.category.DEFAULT" />
                
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
            
</intent-filter>
        
</activity>
        
        
<activity android:name="TitleEditor"
                  
android:label="@string/title_edit_title"
                  
android:theme="@android:style/Theme.Dialog">
            
<intent-filter android:label="@string/resolve_title">
                
<action android:name="com.android.notepad.action.EDIT_TITLE" />
                
<category android:name="android.intent.category.DEFAULT" />
                
<category android:name="android.intent.category.ALTERNATIVE" />
                
<category android:name="android.intent.category.SELECTED_ALTERNATIVE"/>
                
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
            
</intent-filter>
        
</activity>
        
    
</application>
</manifest>

 

第一个 activity, NoteList, 和其它 activity 不同,因为它操作一个笔记的目录 (笔记列表), 而不是一个单独的笔记。它一般作为该程序的初始界面。它可以做以下三件事:

  1. <intent-filter>
        
    <action android:name="android.intent.action.MAIN" />
        
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    该 filter 声明了记事本应用程序的主入口。标准的 MAIN action 是一个不需要任何其它信息 (例如数据等) 的程序入口,LAUNCHER category 表示该入口应该在应用程序启动器中列出.

  2. <intent-filter>
        
    <action android:name="android.intent.action.VIEW" />
        
    <action android:name="android.intent.action.EDIT" />
        
    <action android:name="android.intent.action.PICK" />
        
    <category android:name="android.intent.category.DEFAULT" />
        
    <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
    </intent-filter>

    该 filter 声明了改 activity 可以对一个笔记目录做的事情。它允许用户查看或编辑该目录 (使用 VIEW 和 EDIT action), 或者选取特定的笔记 (使用 PICK action).

    <data> 元素的 mimeType 指定了这些 action 可以操作的数据类型。它表明该 activity 可以从一个持有记事本数据的 content provider (vnd.google.note) 取得一个或多个数据项的 Cursor (vnd.android.cursor.dir).

    注意该 filter 提供了一个 DEFAULT category. 这是因为 Context.startActivity() 和 Activity.startActivityForResult () 方法将所有的 intent 都作为作为包含了 DEFAULT category 来处理,只有两个例外:

    • 显式指明目标 activity 名称的 intent.
    • 包含 MAIN action 和 LAUNCHER category 的 intent.

    因此,除了 MAIN 和 LAUNCHER 的 filter 之外,DEFAULT category 是必须的.

  3. <intent-filter>
        
    <action android:name="android.intent.action.GET_CONTENT" />
        
    <category android:name="android.intent.category.DEFAULT" />
        
    <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
    </intent-filter>

    这个 filter 描述了该 activity 能够在不需要知道目录的情况下返回用户选择的一个笔记的能力. GET_CONTENT action 和 PICK action 相类似。在这两者中,activity 都返回用户选择的笔记的 URI. (返回给调用 startActivityForResult () 来启动 NoteList activity 的 activity.) 在这里,调用者指定了用户选择的数据类型而不是数据的目录.

    这个数据类型, vnd.android.cursor.item/vnd.google.note, 表示了该 activity 可以返回的数据类型 -- 一个笔记的 URI. 从返回的 URI, 调用者可以从持有笔记数据的 content provider (vnd.google.note) 得到一个项目 (vnd.android.cursor.item) 的 Cursor.

    也就是说,对于 PICK 来说,数据类型表示 activity 可以给用户显式的数据类型。对于 GET_CONTENT filter, 它表示 activity 可以返回给调用者的数据类型.

 

下列 intent 可以被 NoteList activity 接受:

action:  android.intent.action.MAIN
不指定任何数据直接启动 activity.

action:  android.intent.action.MAIN
category:  android.intent.category.LAUNCHER
不指定任何数据直接启动 activity. 这是程序启动器使用的 intent. 所有使用该组合的 filter 的 activity 被加到启动器中.

action:  android.intent.action.VIEW
data:  content://com.google.provider.NotePad/notes
要求 activity 显示一个笔记列表,这个列表位于 content://com.google.provider.NotePad/notes. 用户可以浏览这个列表并获取列表项的信息.

action:  android.intent.action.PICK
data:  content://com.google.provider.NotePad/notes
请求 activity 显示 content://com.google.provider.NotePad/notes 下的笔记列表。用户可以选取一个笔记,activity 将返回笔记的 URI 给启动 NoteList 的 activity.

action:  android.intent.action.GET_CONTENT
data type:  vnd.android.cursor.item/vnd.google.note
请求 activity 提供记事本数据的一项.

 

 

第二个 activity, NoteEditor, 为用户显示一个笔记并允许他们编辑它。它可以做以下两件事:

  1. <intent-filter android:label="@string/resolve_edit">
        
    <action android:name="android.intent.action.VIEW" />
        
    <action android:name="android.intent.action.EDIT" />
        
    <action android:name="com.android.notepad.action.EDIT_NOTE" />
        
    <category android:name="android.intent.category.DEFAULT" />
        
    <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
    </intent-filter>

    这个 activity 的主要目的是使用户编辑一个笔记 --VIEW 或者 EDIT 一个笔记. (在 category 中,EDIT_NOTE 是 EDIT 的同义词.) intent 包含匹配 MIME 类型 vnd.android.cursor.item/vnd.google.note 的 URI-- 也就是某一个特定的笔记的 URI. 它一般来说是 NoteList activity 中的 PICK 或者 GET_CONTENT action 返回的.

    像以前一样,该 filter 列出了 DEFAULT category.

  2. <intent-filter>
        
    <action android:name="android.intent.action.INSERT" />
        
    <category android:name="android.intent.category.DEFAULT" />
        
    <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
    </intent-filter>

    该 activity 的第二个目的是使用户能够创建一个新的笔记,并插入到已存在的笔记目录中。该 intent 包含了匹配 vnd.android.cursor.dir/vnd.google.note 的 URI, 也就是笔

 

有了这些能力,NoteEditor 就可以接受以下 intent:

action:  android.intent.action.VIEW
data:  content://com.google.provider.NotePad/notes/ID
要求 activity 显示给定 ID 的笔记.

action:  android.intent.action.EDIT
data:  content://com.google.provider.NotePad/notes/ID
要求 activity 显示指定 ID 的笔记,然后让用户来编辑它。如果用户保存了更改,则 activity 更新该 content provider 的数据.

action:  android.intent.action.INSERT
data:  content://com.google.provider.NotePad/notes
要求 activity 创建一个新的空笔记在 content://com.google.provider.NotePad/notes, 并允许用户编辑它,如果用户保存了更改,则该 URI 被返回给调用者.

最后一个 activity, TitleEditor, 允许用户编辑笔记的标题。这可以通过直接调用 activity (在 intent 中设置组件名称) 的方式来实现。但是这里我们用这个机会来展示如何在已有数据上进行另外的操作 (类似于 windows 中的打开方式 -> 程序列表 -- 译者注):

<intent-filter android:label="@string/resolve_title">
    
<action android:name="com.android.notepad.action.EDIT_TITLE" />
    
<category android:name="android.intent.category.DEFAULT" />
    
<category android:name="android.intent.category.ALTERNATIVE" />
    
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
    
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
它必须在一个特定的笔记上调用 (data type  vnd.android.cursor.item/vnd.google.note), 就像之前的 VIEWEDIT action 一样。然而,这里 activity 显示笔记数据中包含的标题,而不是内容.

 

除了支持 DEFAULT category 之外,title 编辑器还支持了另外两个 category: ALTERNATIVE SELECTED_ALTERNATIVE. 这些 category 标志着 activity 可以在选项菜单中呈现给用户 (就像 LAUNCHER category 表示 activity 可以在程序启动器中一样). 注意 filter 还提供了一个显示标签 (android:label="@string/resolve_title") 来更好的控制用户在选项菜单中看到的内容.

 

有了这些能力,以下的 intent 就可以被 TitleEditor 接受:

action:  com.android.notepad.action.EDIT_TITLE
data:  content://com.google.provider.NotePad/notes/ID
要求 activity 显示给定笔记 ID 的标题,并允许用户编辑该标题.

http://blog.csdn.net/lmhit/article/details/5576250

android – Activity.finishAffinity()vs Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK

android – Activity.finishAffinity()vs Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK

在 Android中,如果要清除当前的活动堆栈并启动新的活动(例如,注销应用程序并启动活动中的日志),似乎有两种方法.

如果您的目标API级别超过16,是否有其他优势?

1)完成亲和力

从Activity调用finishAffinity().
Activity.finishAffinity

2)目的标志

Intent intent = new Intent(this,LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

finishAffinity()方法适用于> = API 16.

Intent标志方法适用于> = API 11.

为了清楚,为了清除当前的活动堆栈,这两种方法似乎也是一样的.我的问题是,有人遇到问题,因此有什么理由选择一个呢?

解决方法

功能上没有什么区别,但是在GenyMotion上测试出来似乎有一点微小的视觉差异.参见网络演示: https://drive.google.com/file/d/0B8Y77sY7Y2CGRS02c3UyNjd2MGs/view?usp=sharing

您需要尝试在一系列设备上查看它是否一致.

主观地说,我会用finishAffinity()来表示,因为它更加明确.但是,如果您必须支持< SDK 16你真的没有选择.

android – Intent.FLAG_ACTIVITY_CLEAR_TOP和Intent.FLAG_ACTIVITY_CLEAR_TOP之间有什么区别吗? Intent.FLAG_ACTIVITY_SINGLE_TOP

android – Intent.FLAG_ACTIVITY_CLEAR_TOP和Intent.FLAG_ACTIVITY_CLEAR_TOP之间有什么区别吗? Intent.FLAG_ACTIVITY_SINGLE_TOP

我想知道,2代码有什么区别?

newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

FLAG_ACTIVITY_CLEAR_TOP

If set,and the activity being launched is already running in the
current task,then instead of launching a new instance of that
activity,all of the other activities on top of it will be closed and
this Intent will be delivered to the (Now on top) old activity as a
new Intent.

FLAG_ACTIVITY_SINGLE_TOP

If set,the activity will not be launched if it is already running at
the top of the history stack.

我的理解是

> FLAG_ACTIVITY_CLEAR_TOP – 清除顶部的所有活动,并在同一任务堆栈中阻止同一活动的多个实例.
> FLAG_ACTIVITY_SINGLE_TOP – 在同一任务堆栈中防止同一个Activity的多个实例.

如果我的理解是正确的,那就不是Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP似乎多余了?

我们可以写Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP?

解决方法

FLAG_ACTIVITY_CLEAR_TOP将创建一个新活动并关闭其他活动. FLAG_ACTIVITY_SINGLE_TOP将打开/重新打开该活动,具体取决于该活动是否已启动. 新的意图将在onNewIntent方法中接收,在这两种情况下,但第一个将破坏其他活动.我们需要想象有一个堆栈. 该堆栈由您开始活动的订单组成.想想那个场景.启动活动A,然后启动活动B,然后启动活动C. 堆栈就是这样的: _活动C. _活动B. _活动A. 然后,如果您启动活动A与FLAG_ACTIVITY_CLEAR_TOP,则活动A顶部的所有活动将被关闭,意图将在NewIntent上传递.

android – intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,bmp)图像偏离中心

android – intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,bmp)图像偏离中心

在我的Android桌面上创建快捷方式时,我遇到了一些问题.

首先,我有一个72×72图标,我从SD卡加载到Bitmap对象.

使用该位图对象,我将其设置为我的图标资源.

我遇到的问题是当我设置它时,快捷方式上的图像偏离中心并被切断.从屏幕指标我得到72×72的大小,不知道交易是什么.

码:

Bitmap theBitmap = BitmapFactory.decodeFile("/sdcard/icon.png");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, theBitmap)

我已经尝试过调整它并使用画布,可绘制和另一个位图使其工作,但是当重新启动手机时它恢复到一个小尺寸.

使用与可绘制资源相同的图标使其看起来很完美,但它不是动态的:

Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

使用三星Epic 4g w / 2.1

解决方法:

我有一个类似的问题.我查看了Launcher源代码,我发现如果初始位图大小太小,会出现导致图标无法正确显示的错误.

首先将位图缩放到128×128:

Bitmap scaledBitmap = Bitmap.createScaledBitmap(theBitmap, 128, 128, true);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);

它会解决它.

关于使用 Javascript 和 Android Intent 启动 TOMTOM 导航js启动app的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android Intent and Intent Filter、android – Activity.finishAffinity()vs Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK、android – Intent.FLAG_ACTIVITY_CLEAR_TOP和Intent.FLAG_ACTIVITY_CLEAR_TOP之间有什么区别吗? Intent.FLAG_ACTIVITY_SINGLE_TOP、android – intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,bmp)图像偏离中心等相关知识的信息别忘了在本站进行查找喔。

本文标签: