編輯:關於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(){ 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(); } } } }
在我們開發android app時,會有很多效果都需要模仿IOS,最近在做一個頁面時,其中用到了 ScrollView,但要做成IOS那種在ScrollView滑動時,浮
給大家分享一個高仿微信的PopupWindow、就是微信的掃一掃那個功能窗口、下面有應用運行效果圖、更加直觀的展示了Demo的效果、源代碼是通過兩種方法實現的、大家可以下
一、Android中的事件處理方法事件處理:響應用戶UI動作,提高應用程序交互性1、基於監聽的事件處理機制2、基於回調的事件處理機制3、Handler消息處理
核心思想是通過BitmapFactory.Options來縮放圖片,主要是用到了它的inSampleSize參數(采樣率)當inSampleSize為1的時候,采樣後的圖