码迷,mamicode.com
首页 > 移动开发 > 详细

android服务和广播的结合使用demo

时间:2014-11-24 12:02:12      阅读:249      评论:0      收藏:0      [点我收藏+]

标签: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
	}

	@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<Void,Void,Void>(){
					List<ContactInfo> 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<Void,Void,Void>(){

									@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();
		}
			
		}
		
	}

}

android服务和广播的结合使用demo

标签:android   服务   广播   

原文地址:http://blog.csdn.net/u014600432/article/details/41440909

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!