我正在尝试将一个意图字符串/Download/income_tax_return.pdf
作为广播从App A发送到App B.在App A中,我有以下MainActivity。
package com.mysender;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent=new Intent();
intent.setAction("com.mysender.Data");
intent.putExtra("path", "/Download/income_tax_return.pdf");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(new ComponentName("com.example.app","com.example.app.MainActivity"));
sendBroadcast(intent);
}
}
初始化意图后,我将意图的操作设置如下:com.mysender.Data
并将意图作为广播发送到应用程序B com.example.app
但是在启动App A然后启动App BI时不是
从MyReceiver获取Toast消息。如何将App A的意图发送到App B?我没有收到任何错误。
以及下面的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mysender">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在App B中,我在MainActivity中执行以下操作:
P
ackage com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Receive broad Cast fromn External App
IntentFilter filter = new IntentFilter("com.mysender.Data");
registerReceiver(myReceiver, filter);
}
private MyReceiver myReceiver =new MyReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String path = intent.getStringExtra("path");
Toast.makeText(context,"Data Received from External App: " + path, Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
}
AndroidManifest.xml App B:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.app.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>
</application>
</manifest>
修改
所以我在第二个应用程序的Manifest中注册了MyReceiver但是我仍然没有得到路径字符串:
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.mysender.Data" />
</intent-filter>
</receiver>
答案 0 :(得分:0)
我认为您必须在第二个应用中注册意图过滤器,否则它将不会监听您发送的意图。
答案 1 :(得分:0)
我的代码有效我必须删除以下行:
intent.setComponent(new ComponentName("com.example.app","com.example.app.MainActivity"));
它甚至可以在App B的清单中注册接收器。