我想在两个应用程序之间实现有序意图(sendOrderedBroadcast)。我没有成功尝试过,目的只是接收者在意图发送应用程序而不是第二个应用程序的接收者。任何人都可以提供一些有用的链接或代码,因为我无法通过网络找到它。
发送有序意图的活动(APP 1)
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
Intent in = new Intent("com.example.mnit.ordered");
in.putExtra("data", "hello");
sendOrderedBroadcast(in, null);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
APP-2的清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mnit.orderedpair">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name="com.example.mnit.orderedpair.Receiver1">
<intent-filter android:priority="1">
<action android:name="com.example.mnit.ordered" />
</intent-filter>
</receiver>
<receiver android:name="com.example.mnit.orderedpair.Receiver3">
<intent-filter android:priority="3">
<action android:name="com.example.mnit.ordered" />
</intent-filter>
</receiver>
</application>
</manifest>
APP-2的Recevier1
public class Receiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String res = getResultData();
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/dir1");
dir.mkdirs();
File file = new File(dir, "calllogs.txt");
FileOutputStream fos=new FileOutputStream(file);
fos.write(res.getBytes());
fos.close();
} catch (Exception e)
{
}
Toast.makeText(context, "RCVR 1.." + res, Toast.LENGTH_LONG).show();
}
}
APP-2的接收者2
public class Receiver3 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String s = intent.getExtras().getString("data");
Toast.makeText(context, "RCVR 3.." + s, Toast.LENGTH_LONG).show();
setResultData(s);
}
}