发送通知,需要先新建一个通知的类:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("接收到了消息,消息的内容是:"+intent.getStringExtra("data"));
}
}
之后使用sendBroadcast发送:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSendMsg).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnSendMsg:
Intent i = new Intent(this,MyReceiver.class);
i.putExtra("data","jikexueyuan");
sendBroadcast(i);
break;
}
}
}