android学习笔记8

BroadcastReceiver:
        在构造Intent时必须用一个全局唯一标志符
        调用sendBroadcast(Intent intent)发送广播 
       
        创建BroadcastReceiver对象需要继承BroadcastReceiver类,
        重写onReceive()方法,在方法中完成业务的调用
       
        BroadcastReceiver会在接受到匹配的广播后消息后,自动调用onReceive方法,其生命周期为5秒;
       
       
        ① 定义BroadcastReceiver     ② 注册广播:xml注册、代码注册
        ③ 重写onReceive()方法,在方法中完成业务的调用      ④ 5秒钟的生命周期
        ⑤ 使用Intent封装业务服务,将action注册名放入构造中
       
        使用Intent封装类名[完成的限定名],将数据放入intent对象当中,调用sendBroadcast方法;
        为了在应用程序中接受广播消息,在xml文件中注册<receiver />
            实例:<receiver android:name=".类名">
                      <intent-filter>
                         <action android:name="随意的服务名字" /><!-- 广播标识 -->
                      </intent-filter>
                  </receiver>
        无需调用BroadcastProvider,无需实例化
       
        示例代码:
           在manifest中注册的Broadcast组件:
             <receiver android:name=".BroadcastHelper">
              <intent-filter>
                   <action android:name="cn.ngh.broadcast.Broadcaster" />
              </intent-filter>
             </receiver>
         
         **在activity类中:
                Intent intent = new Intent("cn.ngh.broadcast.Broadcaster");// 使用Intent封装注册的receiver action名
              intent.putExtra("message", msg); // 在intent中放入信息
              sendBroadcast(intent); // 发布广播,自动调用BroadcastProvider中的onReceive方法
             
         在BroadcastProvider子类中,使用Notification发布通知:
             public class BroadcastHelper extends BroadcastReceiver {
       @Override
       public void onReceive(Context context, Intent intent) {
        NotificationManager manager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.icon,
         "有系统消息!", System.currentTimeMillis());
        // 设置下拉列表
        PendingIntent pi = PendingIntent.getActivity(context, 1,
          new Intent(context, MainActivity.class), 2);
        // 设置消息
        notify.setLatestEventInfo(context, "天气", intent.getStringExtra("message"), pi);
        manager.notify(1, notify);
       }
      }
       
               
    短消息实现:
        ① BroadcastReceive
        ② 系统sms服务封装Intent[action,数据]
       
        注册系统sms的action组件:android.provider.Telephony.SMS_RECEIVED
        注册使用权限:android.permission.BROADCAST_SMS
             例: <uses-permission android:name="android.permission.BROADCAST_SMS" />
            
       
        代码注册broadcast:
              实例化BroadcastReceiver的子类,同时创建IntentFilter对象,使用IntentFilter对象addAction(String actionName)
              最后调用this.registReceiver()方法注册
             
        SmsManager smsManager = SmsManager.getDefault();// 获取内容
        实现Broadcast接受手机短信并转发内容:
            public class SMSReceiverListener extends BroadcastReceiver {
       @Override
       public void onReceive(Context context, Intent intent) {
        Log.i("resultCode==========", "" + getResultCode());
        Log.i("resultData==========", "" + getResultData());
        /** sms管理器 */
        Bundle bundle = intent.getExtras();
        Object[] datas = (Object[]) bundle.get("pdus");
        SmsMessage[] msgs = new SmsMessage[datas.length];
        String msgContent = "";
        for (int i = 0; i < datas.length; i++) {
         msgs[i] = SmsMessage.createFromPdu((byte[]) datas[i]);
         Log.i("text#######", msgs[i].getDisplayMessageBody());
         msgContent += "from:" + msgs[i].getOriginatingAddress() + "content:" + msgs[i].getMessageBody();
        }
        SmsManager smsManager = SmsManager.getDefault();
        PendingIntent pi = PendingIntent.getActivity(context, 1, new Intent(), 0);
        smsManager.sendTextMessage("5556", "5554", msgContent, pi, null);
       }
             }
       
    
   
     注册intent-filter
           <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
     [
       添加使用权限:
            <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     ]
     在Broadcast中启动音乐播放,使用startActivity前在Broadcast中使用context.setFlags(FLAG_ACTIVITY_NEW_TASK);
     startActivity(Intent intent);
     
     
     Service:    
        android.app.Service
        android.content.ContentProvider
        android.app.Activity
        android.content.BroadcastReceiver
       
        要写一个Service,需要继承Service类,重写方法:
            [onBind()]/onCreate()/onStart()/onDestroy()/
           
            Context.bindServcie()=>Context.startService()=>Context.stopService()/Service.stopSelf()
            onCreate()==============>onStart()===============>onDestroy()

你可能感兴趣的:(android,String,object,service,action,sms)