android服務和廣播的結合使用demo

來源:互聯網
上載者:User

標籤:android   服務   廣播   

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}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic 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();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {if(D){Log.i("StopStrangerPhoneNumberSmsService","onStartCommand");}return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {if(D){Log.i("StopStrangerPhoneNumberSmsService","onDestroy() ");}//取消廣播接收者unregisterReceiver(mReceiver);mReceiver=null;super.onDestroy();}//陌生人簡訊攔截廣播接受者class  StopStrangerSmsReceiver extends BroadcastReceiver{private String sender;//發送人號碼private String body;//簡訊內容@Overridepublic 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<Void,Void,Void>(){List<ContactInfo> infos=null;@Overrideprotected Void doInBackground(Void... params) {ContactInfoProvider contactInfoProvider=new ContactInfoProvider(getApplicationContext());infos=contactInfoProvider.getSystemContactInfos();return null;}@Overrideprotected 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<Void,Void,Void>(){@Overrideprotected Void doInBackground(Void... params) {RubishSmsInfo sms=new RubishSmsInfo();sms.setPhoneNumber(sender);sms.setSmsContent(body);//把陌生人簡訊儲存在資料庫表裡RubishSmsInfoDao rubishSmsInfoDao=new RubishSmsInfoDao(getApplicationContext());rubishSmsInfoDao.addSmsInfos(sms);return null;}@Overrideprotected void onPostExecute(Void result) {// TODO Auto-generated method stubsuper.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();}}}}

android服務和廣播的結合使用demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.