編輯:關於Android編程
1、sendBroadcast方法發送廣播。
package com.example.android_broadcast_normal; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)this.findViewById(R.id.button1); //發送廣播 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,MyReceiver.class); sendBroadcast(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
/** *Version: *author:YangQuanqing *Data: */ package com.example.android_broadcast_normal; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Notification; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Build; /** * 廣播的生命周期onReceive()從開始到結束 * @author YangQuanqing yqq * */ @SuppressLint("NewApi") public class MyReceiver extends BroadcastReceiver { //通知管理實例 private NotificationManager notificationManager; //創建通知者實例 private Notification.Builder builder; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //獲得NotificationManager實例 notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); //實例化創建通知者實例 builder=new Notification.Builder(context); //設置通知 builder.setContentTitle("短信通知"); builder.setContentText("你好"); builder.setSmallIcon(R.drawable.ic_launcher); //構建一個通知 notificationManager.notify(10086,builder.build()); } }
2、發送順序廣播
package com.example.android_brocast_order; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn1,btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=(Button)this.findViewById(R.id.button1); btn2=(Button)this.findViewById(R.id.button2); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 多個廣播接收者發送廣播 Intent intent=new Intent(); intent.putExtra("name", "李華"); intent.setAction("com.yqq.mutilBrocast"); sendBroadcast(intent); } }); //發送有序廣播 btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //創建意圖 Intent intent=new Intent(); intent.putExtra("name","android"); intent.setAction("com.yqq.mutilBrocast"); //發送有序廣播 sendOrderedBroadcast(intent, null); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
/** *Version: *author:YangQuanqing *Data: */ package com.example.android_brocast_order; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * @author YangQuanqing yqq * */ public class BroadCast1 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("BroadCast1<<<", intent.getStringExtra("name")); //abortBroadcast();//終止廣播 } }
/** *Version: *author:YangQuanqing *Data: */ package com.example.android_brocast_order; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * @author YangQuanqing yqq * */ public class CopyOfBroadCast2 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("BroadCast2<<<", intent.getStringExtra("name")); abortBroadcast();//若終止發有序廣播的時候,後面的廣播收不到 } }
/** *Version: *author:YangQuanqing *Data: */ package com.example.android_brocast_order; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * @author YangQuanqing yqq * */ public class CopyOfBroadCast3 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("BroadCast3<<<", intent.getStringExtra("name")); } }
package com.example.yqqmobilesafe.service; import java.util.List; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.AsyncTask; import android.os.IBinder; import android.telephony.SmsMessage; import android.util.Log; import com.example.yqqmobilesafe.RubishSmsActivity; import com.example.yqqmobilesafe.ContactProvider.ContactInfoProvider; import com.example.yqqmobilesafe.db.dao.RubishSmsInfoDao; import com.example.yqqmobilesafe.domain.ContactInfo; import com.example.yqqmobilesafe.domain.RubishSmsInfo; /** * 陌生短信攔截服務 * @author yqq * */ public class StopStrangerPhoneNumberSmsService extends Service { private boolean D=true; private StopStrangerSmsReceiver mReceiver;//陌生人短信攔截廣播接受者 public StopStrangerPhoneNumberSmsService() { // TODO Auto-generated constructor stub } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { if(D){ Log.i("StopStrangerPhoneNumberSmsService"," onCreate()"); } //注冊廣播 mReceiver=new StopStrangerSmsReceiver(); IntentFilter filter=new IntentFilter(); filter.setPriority(Integer.MAX_VALUE); filter.addAction("android.provider.Telephony.SMS_RECEIVED"); registerReceiver(mReceiver, filter); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if(D){ Log.i("StopStrangerPhoneNumberSmsService","onStartCommand"); } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { if(D){ Log.i("StopStrangerPhoneNumberSmsService","onDestroy() "); } //取消廣播接收者 unregisterReceiver(mReceiver); mReceiver=null; super.onDestroy(); } //陌生人短信攔截廣播接受者 class StopStrangerSmsReceiver extends BroadcastReceiver{ private String sender;//發送人號碼 private String body;//短信內容 @Override public void onReceive(Context context, Intent intent) { Object[] objs = (Object[]) intent.getExtras().get("pdus"); for(Object obj : objs ){ SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj); //獲得短信發送者的號碼 sender = smsMessage.getOriginatingAddress(); body = smsMessage.getMessageBody(); new AsyncTask(){ List infos=null; @Override protected Void doInBackground(Void... params) { ContactInfoProvider contactInfoProvider=new ContactInfoProvider(getApplicationContext()); infos=contactInfoProvider.getSystemContactInfos(); return null; } @Override protected void onPostExecute(Void result) { if(infos==null){ //Toast.makeText(getApplicationContext(),, duration) return; } for(ContactInfo info:infos){ if(info.getPhoneNumber().equals(sender)){ return; } } abortBroadcast(); new AsyncTask (){ @Override protected Void doInBackground( Void... params) { RubishSmsInfo sms=new RubishSmsInfo(); sms.setPhoneNumber(sender); sms.setSmsContent(body); //把陌生人短信存儲在數據庫表裡 RubishSmsInfoDao rubishSmsInfoDao=new RubishSmsInfoDao(getApplicationContext()); rubishSmsInfoDao.addSmsInfos(sms); return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent=new Intent(StopStrangerPhoneNumberSmsService.this,RubishSmsActivity.class); PendingIntent pi=PendingIntent.getActivity(StopStrangerPhoneNumberSmsService.this, 0, intent, 0); //發一個攔截通知 Notification notification=new Notification.Builder(getApplicationContext()) .setAutoCancel(true) .setTicker("攔截到陌生短信") .setSmallIcon(com.example.yqqmobilesafe.R.drawable.notif_icon_sms) .setContentText("攔截到陌生短信") .setContentIntent(pi) .build(); nm.notify(100,notification); } }.execute(); super.onPostExecute(result); } }.execute(); } } } }
首先看程序界面如下!1、布局文件:復制代碼 代碼如下:<AbsoluteLayout xmlns:android=http://schemas.android.co
電腦版qq添加好友估計大家都很熟悉了,那麼手機qq能添加好友嗎?手機qq怎麼添加好友,下面就隨小編一起來看看吧! 手機qq添加好友教程 1.打開手機QQ,利
本文實例講述了Android實現學生管理系統,分享給大家供大家參考。具體如下:(1)管理系統實現的功能主要是:學生、教師的注冊登錄,和選課,以及修改學生的成績等基本簡單的
在一些音樂類應用中, 經常會展示隨著節奏上下起伏的波紋信息, 這些波紋形象地傳達了聲音信息, 可以提升用戶體驗, 那麼是如何實現的呢? 可以使用Visualize