布局,在布局中添加一个简单的按钮:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doSendBroadcast"
android:text="发送普通广播" />public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doSendBroadcast(View view){
Intent intent = new Intent();
intent.putExtra("data", "data = " + new Date());
intent.setAction("ACTION_TEST_BROADCAST");
sendBroadcast(intent);
}
}public class TestBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println(intent.getStringExtra("data"));
}
}<receiver
android:name="com.example.lianxi.TestBroadcastReceiver">
<intent-filter >
<action android:name="ACTION_TEST_BROADCAST"/>
</intent-filter>
</receiver> public class MainActivity extends Activity {
private BroadcastReceiver receiver = new InnerBroadcastReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction("ACTION_TEST_BROADCAST");
registerReceiver(receiver, filter);
}
private class InnerBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println( "InnerBroadcastReceiver" + intent.getStringExtra("data"));
}
}
public void doSendBroadcast(View view){
Intent intent = new Intent();
intent.putExtra("data", "data = " + new Date());
intent.setAction("ACTION_TEST_BROADCAST");
sendBroadcast(intent);
}
//要取消注册,否则的话logcat会报错
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(receiver);
super.onDestroy();
}
} package com.example.lianxi;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private ImageButton play_Button;
private ProgressBar progressbar;
private TextView start_Time, end_Time;
private BroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play_Button = (ImageButton) findViewById(R.id.play);
progressbar = (ProgressBar) findViewById(R.id.music_seek);
start_Time = (TextView) findViewById(R.id.start_time);
end_Time = (TextView) findViewById(R.id.end_time);
play_Button.setOnClickListener(new InnerOnClickListener());
startService(new Intent(this, PlayMusicService.class));
receiver = new InnerBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.edu.hpu.intent.action.UPDATE_PROGRESS");
filter.addAction("com.edu.hpu.SET PAUSE_BUTTON");
filter.addAction("com.edu.hpu.SET PLAY_BUTTON");
registerReceiver(receiver, filter);
}
private class InnerOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.edu.hpu.intent.action.PLAY_MUSIC");
sendBroadcast(intent);
}
}
private class InnerBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if("com.edu.hpu.SET PAUSE_BUTTON".equals(intent.getAction())){
play_Button.setImageResource(android.R.drawable.ic_media_pause);
}else if("com.edu.hpu.SET PLAY_BUTTON".equals(intent.getAction())){
play_Button.setImageResource(android.R.drawable.ic_media_play);
}
else if("com.edu.hpu.intent.action.UPDATE_PROGRESS".equals(intent.getAction())){
int currentPosition = intent.getIntExtra("current_position", 0);
int duration = intent.getIntExtra("duration", 0);
int progress = currentPosition * 100 / duration;
start_Time.setText(getFormattedTime(currentPosition));
end_Time.setText(getFormattedTime(duration));
progressbar.setProgress(progress);
}
}
}
private String getFormattedTime(long date) {
return new SimpleDateFormat("mm:ss", Locale.CHINA)
.format(new Date(date));
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(receiver);
stopService(new Intent(this,PlayMusicService.class));
super.onDestroy();
}
}package com.example.lianxi;
import java.io.IOException;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;
public class PlayMusicService extends Service {
private MediaPlayer player;
private String musicPath;
private int currentPosition;
private BroadcastReceiver receiver;
private boolean isRunning;
@Override
public void onCreate() {
player = new MediaPlayer();
player.setOnPreparedListener(new InnerPreparedListener());
musicPath = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC).toString()
+ "/number1.mp3";
IntentFilter filter = new IntentFilter();
filter.addAction("com.edu.hpu.intent.action.PLAY_MUSIC");
receiver = new InnerBroadcastReceiver();
registerReceiver(receiver, filter);
isRunning = true;
new UpdateProgressThread().start();
}
// 子线程发送广播
private class UpdateProgressThread extends Thread {
@Override
public void run() {
while (isRunning) {
if (player.isPlaying()) {
Intent intent = new Intent(
"com.edu.hpu.intent.action.UPDATE_PROGRESS");
intent.putExtra("current_position",
player.getCurrentPosition());
intent.putExtra("duration", player.getDuration());
sendBroadcast(intent);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private class InnerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent it = new Intent();
if (player.isPlaying()) {
pause();
it = new Intent("com.edu.hpu.SET PLAY_BUTTON");
sendBroadcast(it);
} else {
play();
it = new Intent("com.edu.hpu.SET PAUSE_BUTTON");
sendBroadcast(it);
}
}
}
private void play() {
try {
player.reset();
player.setDataSource(musicPath);
player.prepareAsync();
// player.start();
} catch (IllegalArgumentException e) {
// TODO: handle exception
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void pause() {
if (player.isPlaying()) {
currentPosition = player.getCurrentPosition();
player.pause();
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
private class InnerPreparedListener implements
MediaPlayer.OnPreparedListener {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
if (currentPosition != 0) {
mp.seekTo(currentPosition);
}
mp.start();
currentPosition = 0;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
player.release();
player = null;
isRunning = false;
unregisterReceiver(receiver);
super.onDestroy();
}
}<service
android:name="com.example.lianxi.PlayMusicService"></service> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@android:drawable/ic_media_play" />
<ProgressBar
android:id="@+id/music_seek"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/start_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="left"
android:text="00:00" />
<TextView
android:id="@+id/end_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:gravity="right"
android:text="00:00" />
</RelativeLayout>
</LinearLayout>MainActivity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this,SmsReceiverService.class));
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
stopService(new Intent(this,SmsReceiverService.class));
super.onDestroy();
}
} public class SmsReceiverService extends Service{
private BroadcastReceiver receiver;
@Override
public void onCreate() {
// TODO Auto-generated method stub
receiver = new InnerBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, filter);
}
private class InnerBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Object[] pdus = (Object[])intent.getExtras().get("pdus");
SmsMessage message;
byte[] pdu;
for (int i = 0; i < pdus.length; i++) {
pdu = (byte[]) pdus[i];
message = SmsMessage.createFromPdu(pdu);
String body = message.getMessageBody();
String address = message.getOriginatingAddress();
long time = message.getTimestampMillis();
System.out.println(address);
System.out.println("信息="+body);
System.out.println(new Date(time).toString());
}
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
}
原文:http://blog.csdn.net/wojiaohuangyu/article/details/50449482