码迷,mamicode.com
首页 > 其他好文 > 详细

扣丁学堂笔记第11天广播接收器、短信与闹钟服务

时间:2016-03-28 00:21:34      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

1.默认广播

技术分享

技术分享

MainActivity

package com.example.broadcastreceivertest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void sendNormalClick(View v) {
		Intent intent = new Intent(
				"com.example.broadcastreceivertest.MY_ACTION");
		intent.putExtra("info", "我是一条普通广播");
		this.sendBroadcast(intent);
	}
}

MyBroadcastReceiver

package com.example.broadcastreceivertest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {

	// public MyBroadcastReceiver(){}//构造方法,可以不写

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String info = intent.getStringExtra("info");
		Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
	}

}

AndroidMainfest.xml

<receiver android:name=".MyBroadcastReceiver">
            <intent-filter>
                <action android:name="com.example.broadcastreceivertest.MY_ACTION"/>
            </intent-filter>
        </receiver>

2.代码注册广播接收器

技术分享技术分享技术分享

在onResume()注册广播,在onPause()解除注册广播,注册方法:在onResume外创建一个接收器的实例receiver,然后在onResume创建一个IntentFilter的实例filter,调用filter的addAction方法,再调用registerReceiver方法,传入receiver和filter

解除注册调用unregisterReceiver方法,传入receiver参数



3.有序广播

技术分享技术分享

MainActivity

package com.example.orderbroadcastreceivertest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendOrderClick(View v){
    	Intent intent = new Intent("com.example.orderbroadcastreceivertest.MY_BROADCAST");
    	this.sendOrderedBroadcast(intent, null);
    	
    }

    
    
}

MyReceiver1

package com.example.orderbroadcastreceivertest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver1 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Bundle data = getResultExtras(false);
		String info = data.getString("info");
		Toast.makeText(context, "有序广播--1"+info, Toast.LENGTH_SHORT).show();
	}
	
	
}

MyReceiver2

package com.example.orderbroadcastreceivertest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver2 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "有序广播--2", Toast.LENGTH_SHORT).show();
		
		Bundle data = new Bundle();
		data.putString("info", "广播传值");
		this.setResultExtras(data);
		
		this.abortBroadcast();
	}
}

AndroidMainfest.xml

<receiver android:name=".MyReceiver1">
            <intent-filter android:priority="100">
                <action android:name="com.example.orderbroadcastreceivertest.MY_BROADCAST"></action>
            </intent-filter>
        </receiver>
        
        <receiver android:name=".MyReceiver2">
            <intent-filter android:priority="200">
                <action android:name="com.example.orderbroadcastreceivertest.MY_BROADCAST"></action>
            </intent-filter>
        </receiver>


4.粘性广播

技术分享


5.接收开机启动广播和网络状态变化广播

技术分享技术分享

注:<action android:name="android.net.conn.CONNECTIVITY_CHAGE"/>不是系统提供的,要手动添加


6.接收电量变化广播

技术分享技术分享


7.发送短信

技术分享

MainActivity

package com.example.smstest;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void sendClick(View v) {

		// 获取短信管理器
		android.telephony.SmsManager manager = android.telephony.SmsManager
				.getDefault();
		String sms = "我是一条短信";

		// 拆分长短信
		ArrayList<String> text = manager.divideMessage(sms);

		for (int i = 0; i < text.size(); i++) {
			manager.sendTextMessage("131****2160", null, text.get(i), null,
					null);
		}
	}
}

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>


8.接收短信

技术分享技术分享


9.AlarmManager服务

技术分享

技术分享

MainActivity

package com.example.alarmtest;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void startClick(View v) {
		AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		long triggerTime = System.currentTimeMillis() + 3000;
		Intent intent = new Intent(this, MyReceiver.class);
		PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		am.set(AlarmManager.RTC, triggerTime, pi);
		// am.setRepeating(AlarmManager.RTC, triggerTime, 8000, pi);
	}
}

MyReceiver

package com.example.alarmtest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "起床啦!", Toast.LENGTH_LONG).show();
	}

}

<receiver android:name=".MyReceiver"></receiver>

技术分享


扣丁学堂笔记第11天广播接收器、短信与闹钟服务

标签:

原文地址:http://blog.csdn.net/huangluqian/article/details/50990273

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