android 短信拦截 实例完整源码下载
可实现拦截短信,自动转发的功能。 部分代码:import android.app.AlarmManager;import android.app.PendingIntent;import android.app.Service;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.IBinder;import android.os.PowerManager;import android.os.PowerManager.WakeLock;public class MyService extends Service{ SMSReceiver rs=null; static MyService service; private static PendingIntent pi = null; public static AlarmManager am = null; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); service=this; pm = (PowerManager) getSystemService(Context.POWER_SERVICE); registerAlarm(); acquireWakeLock(this); registerRecever(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); releaseWakeLock(); unregisterAlarm(); } private void registerRecever(){ rs=new SMSReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("android.provider.Telephony.SMS_RECEIVED"); filter.setPriority(1000); registerReceiver(rs, filter); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub if(rs==null)registerRecever(); return START_STICKY; } public static WakeLock wakeLock = null; private static PowerManager pm = null; public static synchronized void acquireWakeLock(Context c) { if (null == wakeLock) { wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "smslock"); wakeLock.acquire(); } } public static synchronized void releaseWakeLock() { if (wakeLock == null) return; if (wakeLock.isHeld()) { wakeLock.release(); wakeLock = null; } } /** * 启动重复型定时器 */ public static void registerAlarm() { if(Config.getAlarm())return; Intent intent = new Intent(service, AlarmReceiver.class); pi = PendingIntent.getBroadcast(service, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); // 设置一个PendingIntent对象,发送广播 am = (AlarmManager) service.getSystemService(Context.ALARM_SERVICE); // 获取AlarmManager对象, Config.setAlarm(true); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30*60*1000, pi); } /** * 取消定时器 */ public static void unregisterAlarm() { am.cancel(pi); am = null; Config.setAlarm(false); }}
- 2014-05-21下载
- 积分:1