Èç¹û°ÑÉú»î±ÈÓ÷Ϊ´´×÷µÄÒâ¾³£¬ÄÇôÔĶÁ¾ÍÏñÑô¹â¡£
±¾½²ÄÚÈÝ£º¾Ù¼¸¸ö³£¼ûµÄÀý×Ó¼ÓÉîһ϶ÔBroadcastReceiver¹ã²¥µÄÀí½âºÍÓ¦Óãº
Ò»¡¢¿ª»úÆô¶¯·þÎñ
ÎÒÃǾ³£»áÓÐÕâÑùµÄÓ¦Óó¡ºÏ£¬±ÈÈçÏûÏ¢ÍÆËÍ·þÎñ£¬ÐèҪʵÏÖ¿ª»úÆô¶¯µÄ¹¦ÄÜ¡£ÒªÊµÏÖÕâ¸ö¹¦ÄÜ£¬ÎÒÃǾͿÉÒÔ¶©ÔÄϵͳ¡°Æô¶¯Íê³É¡±ÕâÌõ¹ã²¥£¬½ÓÊÕµ½ÕâÌõ¹ã²¥ºóÎÒÃǾͿÉÒÔÆô¶¯×Ô¼ºµÄ·þÎñÁË¡£ÎÒÃÇÀ´¿´Ò»ÏÂBootCompleteReceiverºÍMsgPushServiceµÄ¾ßÌåʵÏÖ£º
public class BootCompleteReceiver extends BroadcastReceiver { private static final String TAG = "MyReceiver"; @Override public void onReceive(Context context, Intent intent) { Intent service =new Intent(context,MsgPushService.class); context.startService(service); Log.i(TAG, "Boot Complete. Starting MsgPushService..."); } }
public class MsgPushService extends Service{ private static final String TAG = "MyReceiver"; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate called."); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand called."); return super.onStartCommand(intent, flags, startId); } }
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
ÎÒÃÇ¿´µ½BootCompleteReceiver×¢²áÁË¡°android.intent.action.BOOT_COMPLETED¡±Õâ¸ö¿ª»ú¹ã²¥µØÖ·£¬´Ó°²È«½Ç¶È¿¼ÂÇ£¬ÏµÍ³ÒªÇó±ØÐëÉùÃ÷½ÓÊÕ¿ª»úÆô¶¯¹ã²¥µÄȨÏÞ£¬ÓÚÊÇÎÒÃÇÔÙÉùÃ÷ʹÓÃÏÂÃæµÄȨÏÞ£º
Èç¹ûÎÒÃDz鿴ÒÑÔËÐеķþÎñ¾Í»á·¢ÏÖ£¬MsgPushServiceÒѾÔËÐÐÆðÀ´ÁË¡£
<†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPrb+oaLN+MLn17TMrLHku688L3A+CjxwPgrGqcjn08O7p+SvwMDN+MLn0MXPosqxo6zN+MLnzbvIu7bPv6qjrM7Sw8fSqrywyrG12Mzh0NHTw7unzfjC59LRts+/qqGj0qrKtc/W1eK49rmmxNyjrM/Cw+a+zcC0yrXP1tK7z8LV4rj2uabE3KO6PC9wPgo8cD4KPHByZSBjbGFzcz0="brush:java;">public class NetworkStateReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "network state changed.");
if (!isNetworkAvailable(context)) {
Toast.makeText(context, "network disconnected!", 0).show();
}
}
/**
* ÍøÂçÊÇ·ñ¿ÉÓÃ
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
}
×¢²á
Èý¡¢µçÁ¿±ä»¯
Èç¹ûÎÒÃÇÔĶÁÈí¼þ£¬¿ÉÄÜÊÇÈ«ÆÁÔĶÁ£¬Õâ¸öʱºòÓû§¾Í¿´²»µ½Ê£ÓàµÄµçÁ¿£¬ÎÒÃǾͿÉÒÔΪËûÃÇÌṩµçÁ¿µÄÐÅÏ¢¡£ÒªÏë×öµ½ÕâÒ»µã£¬ÎÒÃÇÐèÒª½ÓÊÕÒ»ÌõµçÁ¿±ä»¯µÄ¹ã²¥£¬È»ºó»ñÈ¡°Ù·Ö±ÈÐÅÏ¢£¬ÎÒÃǾÍÀ´ÊµÏÖÒÔÏ£º
public class BatteryChangedReceiver extends BroadcastReceiver { private static final String TAG = "MyReceiver"; @Override public void onReceive(Context context, Intent intent) { int currLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); // µ±Ç°µçÁ¿ int total = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1); // ×ܵçÁ¿ int percent = currLevel * 100 / total; Log.i(TAG, "battery: " + percent + "%"); } }
public class BatteryChangedReceiver extends BroadcastReceiver { private static final String TAG = "MyReceiver"; @Override public void onReceive(Context context, Intent intent) { Intent batteryIntent = getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int currLevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); int total = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1); int percent = currLevel * 100 / total; Log.i("battery", "battery: " + percent + "%"); } private Context getApplicationContext() { // TODO Auto-generated method stub return null; } }