从零开始学android<BroadCast广播.四十五.>
与Service相似,在android中还有一种运行在后台的4大组件之一,他就是broadCcast,但是他不用考虑是否有接受的对象,只需要考虑发送即可。
要想使用BroadCast我们可以使用两种方法
第一:将broadCcast配置到配置文件当中。
第二:将broadCcast与class文件进行绑定。
下面我们分别来进行说明。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="134dp" android:text="开启通知" /> </RelativeLayout>
广播类
package com.example.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class BroadCastUtil extends BroadcastReceiver { public void BroadcastReceiver(){ System.out.println("**一个广播事件被建立"); } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "广播已经启动", 2).show(); } }主文件
package com.example.broadcast; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 设置过滤 Intent intent = new Intent(Intent.ACTION_EDIT); // 开启广播 MainActivity.this.sendBroadcast(intent); } }); } }
在 application节点下增加 配置
<receiver android:name="BroadCastUtil" android:enabled="true" > <intent-filter ><action android:name="android.intent.action.EDIT"/></intent-filter> </receiver>
使用registerReceiver(BroadcastReceiver, IntentFilter)进行绑定过滤操作
其他的不用进行改变只用改变
主文件 就OK
package com.example.broadcast; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 设置过滤 Intent intent = new Intent("com.example.broadcast"); IntentFilter filter=new IntentFilter("com.example.broadcast"); BroadCastUtil broadCastUtil=new BroadCastUtil(); MainActivity.this.registerReceiver(broadCastUtil, filter); // 开启广播 MainActivity.this.sendBroadcast(intent); } }); } }
这样进行绑定和配置的话,配置文件中就不需要配置信息了 。
使用广播启动Service
Service
package com.example.broadecast2; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub System.out.println("** SERVICE onCreate"); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("** SERVICE onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("** SERVICE onStartCommand"); return Service.START_CONTINUATION_MASK; } }<strong> </strong>BroadCask
package com.example.broadecast2; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBroadeCast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent1) { context.startService(new Intent(context,MyService.class)) ; } }
Main
package com.example.broadecast2; import android.os.Bundle; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.view.Menu; //使用 广播启动服务 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent =new Intent("com.example.broadecast2.DEMO"); MainActivity.this.sendBroadcast(intent); } }
使用广播进行闹钟的控制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" tools:context=".MainActivity" > <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_alignParentRight="true" android:text="取消闹钟" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/timePicker1" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:text="设置闹钟" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_marginBottom="62dp" android:text="当前未设置闹钟" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout><strong> </strong>对话框
package com.example.broadcast3; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; public class AlertMessage extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); new AlertDialog.Builder(this) .setIcon(R.drawable.b9) .setTitle("闹钟时间到") .setMessage( "当前时间为:" + new SimpleDateFormat( "yyyy年MM月dd日 HH时 mm分 ss秒") .format(new Date())) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub AlertMessage.this.finish(); } }).create().show(); } }
广播类
package com.example.broadcast3; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBroadCast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Intent it = new Intent(context, AlertMessage.class); it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(it); } }Main
package com.example.broadcast3; import java.util.Calendar; import android.os.Bundle; import android.R.integer; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; import android.widget.Toast; public class MainActivity extends Activity { private Button set,delete; private TextView info; private TimePicker time; private int hour ; private int minute; private Calendar calendar = Calendar.getInstance() ; private AlarmManager alarm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); set=(Button)this.findViewById(R.id.button1); delete=(Button)this.findViewById(R.id.button2); info=(TextView)this.findViewById(R.id.textView1); time=(TimePicker)this.findViewById(R.id.timePicker1); time.setIs24HourView(true); this.alarm = (AlarmManager) super.getSystemService(Context.ALARM_SERVICE) ; time.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub MainActivity.this.hour=hourOfDay; MainActivity.this.minute=minute; MainActivity.this.calendar.setTimeInMillis(System.currentTimeMillis()) ; MainActivity.this.calendar.set(Calendar.HOUR_OF_DAY, hourOfDay) ; MainActivity.this.calendar.set(Calendar.MINUTE, minute) ; MainActivity.this.calendar.set(Calendar.SECOND, 0) ; MainActivity.this.calendar.set(Calendar.MILLISECOND, 0) ; } }); set.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent =new Intent(MainActivity.this, AlertMessage.class); intent.setAction("com.example.broadcast3"); PendingIntent sender=PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); MainActivity.this.alarm.set(AlarmManager.RTC_WAKEUP, MainActivity.this.calendar.getTimeInMillis(), sender); MainActivity.this.info.setText("闹钟响起的时间是:" + MainActivity.this.hour + "时" + MainActivity.this.minute + "分。"); Toast.makeText(MainActivity.this, "闹钟设置成功!", Toast.LENGTH_LONG).show(); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (MainActivity.this.alarm != null) { Intent intent = new Intent(MainActivity.this, MyBroadCast.class); intent.setAction("org.lxh.action.setalarm") ; PendingIntent sender = PendingIntent.getBroadcast( MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); MainActivity.this.alarm.cancel(sender) ; // 取消 MainActivity.this.info.setText("当前没有设置闹钟。") ; Toast.makeText(MainActivity.this, "闹钟删除成功!", Toast.LENGTH_LONG).show(); } } }); } }
下节预报:android基本绘图
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。