MainActivity.java
package com.jing.example.nknu_.android_broadcast_send;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
Intent intent = new Intent();
intent.setAction("abc");
intent.putExtra("name", "jack");
sendBroadcast(intent);
}
});
}
}
MyBroadcastReceiver.java
package com.jing.example.nknu_.android_broadcast_send;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
/**
* Created by nknu_ on 2016/4/2.
*/
public class MyBroadcastReceiver extends BroadcastReceiver {
private NotificationManager manager;
public MyBroadcastReceiver(){
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("MyBroadcastReceiver: 我收到了");
manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String name = intent.getStringExtra("name");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setTicker("廣播來了");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("沒有網了");
builder.setContentText("wifi掉了" + name);
manager.notify(1001, builder.build());
//Toast.makeText(context, "hello world!!" + name, Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver2.java
package com.jing.example.nknu_.android_broadcast_send;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by nknu_ on 2016/4/2.
*/
public class MyBroadcastReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("MyBroadcastReceiver2: 我收到了");
String name = intent.getStringExtra("name");
Toast.makeText(context, "hello world!!" + name, Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver3
package com.jing.example.nknu_.android_broadcast_send;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by nknu_ on 2016/4/2.
*/
public class MyBroadcastReceiver3 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("MyBroadcastReceiver3: 我收到了");
String name = intent.getStringExtra("name");
Toast.makeText(context, "hello world!!" + name, Toast.LENGTH_LONG).show();
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.jing.example.nknu_.android_broadcast_send.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發送廣播"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>