好贷网好贷款

跨程序之间的广播传值

发布时间:2016-12-4 7:53:33 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"跨程序之间的广播传值",主要涉及到跨程序之间的广播传值方面的内容,对于跨程序之间的广播传值感兴趣的同学可以参考一下。

第一个应用程序:Broadcast 【MainActivity.java】 package com.example.broadcast; import android.os.Bundle; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { public final static String SEND_OK_MESSAGE = "send.ok.message"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(SEND_OK_MESSAGE); intent.putExtra("name", "AnJon"); sendBroadcast(intent); } } 第二个应用程序:Broadcast1 【MainActivity.java】 package com.example.broadcast1; import android.os.Bundle; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent i = new Intent(this, MyService.class); startService(i); } } 【MyService.java】 package com.example.broadcast1; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; import android.widget.Toast; public class MyService extends Service { public final static String SEND_OK_MESSAGE = "send.ok.message"; private BroadcastReceiver myBroadCast = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(SEND_OK_MESSAGE)) { Toast.makeText( context, "接收到了一条广播为" + SEND_OK_MESSAGE + intent.getStringExtra("name"), Toast.LENGTH_LONG).show(); } } }; @Override public void onCreate() { super.onCreate(); } @Override public void onStart(Intent intent, int startId) { IntentFilter myFilter = new IntentFilter(); myFilter.addAction(SEND_OK_MESSAGE); this.registerReceiver(myBroadCast, myFilter); super.onStart(intent, startId); } @Override public IBinder onBind(Intent arg0) { return null; } } 【AndroidManifest.xml】清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcast1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.broadcast1.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> <service android:name=".MyService"> <intent-filter> <action android:name="com.example.broadcast1.MyService"></action> </intent-filter> <intent-filter> <action android:name="send.ok.message" /> </intent-filter> </service> </application> </manifest> 运行效果:----------------------------------》》》》》  》》》》》》》》》

上一篇:jsp复选框的取值问题
下一篇:华为编程 复数求均值

相关文章

相关评论