本文主要是介绍Android 接收系统广播,开机启动Service,SMS,Battery,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文内容摘自《疯狂Android讲义 第3版》李刚 著
自动开启的Service:
package shortcut.song.com.myapplication;import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;import java.util.Timer;
import java.util.TimerTask;/*** Created by Administrator on 2017/8/17 0017.*/public class LauncherService extends Service {Timer mTimer = null;TimerTask mTimerTask = null;public LauncherBind myLauncherBind = new LauncherBind();public class LauncherBind extends Binder {}@Nullable@Overridepublic IBinder onBind(Intent intent) {return myLauncherBind;}@Overridepublic boolean onUnbind(Intent intent) {return super.onUnbind(intent);}@Overridepublic void onCreate() {super.onCreate();mTimer = new Timer();mTimerTask = new TimerTask() {@Overridepublic void run() {System.out.println("======LauncherService:"+ System.currentTimeMillis());}};mTimer.schedule(mTimerTask, 500, 1500);}
}
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;/*** Created by Administrator on 2017/8/17 0017.*/public class LauncherBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Intent tIntent = new Intent(context, LauncherService.class);// 启动指定的Servicecontext.startService(tIntent);}
}
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/><receiver android:name=".LauncherBroadcastReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"/></intent-filter>
</receiver>
SMS:
<uses-permission android:name="android.permission.READ_SMS"/><receiver android:name=".SmsBroadcastReceiver"><intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver>
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;/*** Created by Administrator on 2017/8/17 0017.*/public class SmsBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if ( intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {// 取消广播续继传播放abortBroadcast();StringBuilder sb = new StringBuilder();Bundle bundle = intent.getExtras();if (bundle != null) {Object[] pdus = (Object[]) bundle.get("pdus");SmsMessage[] messages = new SmsMessage[pdus.length];for (int i=0; i < pdus.length ; i++) {messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);}for (SmsMessage message : messages) {sb.append("SMS src:");sb.append(message.getDisplayOriginatingAddress());sb.append(message.getDisplayMessageBody());}}Toast.makeText(context, sb.toString(), Toast.LENGTH_SHORT).show();}}
}
Battery:
<uses-permission android:name="android.permission.BATTERY_STATS"/><receiver android:name=".BatteryReceiver"><intent-filter><action android:name="android.intent.action.BATTERY_CHANGED"/><action android:name="android.intent.action.BATTERY_LOW"/></intent-filter></receiver>
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;/*** Created by Administrator on 2017/8/17 0017.*/public class BatteryReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Bundle bundle = intent.getExtras();int current = bundle.getInt("level");int total = bundle.getInt("scale");if (current * 1.0 / total < 0.15 ){Toast.makeText(context, "Battery Low!!!", Toast.LENGTH_SHORT).show();}}
}
这篇关于Android 接收系统广播,开机启动Service,SMS,Battery的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!