我也需要做类似的事情。我一开始很挣扎,但现在我找到了解决方案,很高兴分享这些信息,这可能对其他人有用
首先你需要写一个cordova插件,这个插件应该有BroadcastReceiver的实现,如下图
public class IntentReceiver extends BroadcastReceiver {
public static final String EXTRA_NAME = "message";
@Override
public void onReceive(Context ctx, Intent intent) {
try{
Intent mainIntent = new Intent(ctx, Class.forName(ctx.getPackageName() + ".MainActivity"));
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String extra = intent.getStringExtra(EXTRA_NAME);
mainIntent.putExtra("message", extra);
ctx.startActivity(mainIntent);
}catch(Exception ex){ }
}
Plugin.xml
将以下节点添加到 plugin.xml 文件
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</config-file>
<config-file parent="/manifest/application" target="AndroidManifest.xml">
<receiver android:name="hcw.fi.phoniro.receiver.IntentReceiver" android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.SEND" />
</intent-filter>
</receiver>
</config-file>
htmlpage.ts
在平台准备好下面的代码
platform.ready().then(() => {
window.plugins.intent.setNewIntentHandler(this.HandleNewIntent);
window.plugins.intent.getCordovaIntent(this.HandleNewIntent, function () {
//alert("Error: Cannot handle open with file intent");
});
});
HandleNewIntent(intent){
if(intent && intent.extras){
intent.extras.myParams) {
// Do something with the File
document.getElementById("intentdata").innerHTML = "Data from Android app : " +intent.extras.message;
}else{
// this will happen in getCordovaIntent when the app starts and there's no
// active intent
console.log("The app was opened manually and there's not file to open");
//alert('The app was opened manually and there is not file to open' + intent);
}
}