程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> µÚÁùÊ®½²£ºËÄ´ó×é¼þÖ®BroadcastReceiver£¨Èý£©

µÚÁùÊ®½²£ºËÄ´ó×é¼þÖ®BroadcastReceiver£¨Èý£©

編輯:C++入門知識

µÚÁùÊ®½²£ºËÄ´ó×é¼þÖ®BroadcastReceiver£¨Èý£©


Èç¹û°ÑÉú»î±ÈÓ÷Ϊ´´×÷µÄÒâ¾³£¬ÄÇôÔĶÁ¾ÍÏñÑô¹â¡£


±¾½²ÄÚÈÝ£º¾Ù¼¸¸ö³£¼ûµÄÀý×Ó¼ÓÉîһ϶Ô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);
	}
}

ÏÂÃæÊÇMainActivity.javaÖ÷½çÃæÎļþ£º

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

ÔÚAndroidManifest.xmlÖÐÅäÖÃ

 
        
            
                
                
                
            
        
        
        
ÎÒÃÇ¿´µ½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; } }

×¢²á


            
                
                
            
        

ÒòΪÔÚisNetworkAvailable·½·¨ÖÐÎÒÃÇʹÓõ½ÁËÍøÂç״̬Ïà¹ØµÄAPI£¬ËùÒÔÐèÒªÉùÃ÷Ïà¹ØµÄȨÏÞÉùÃ÷£º

  

ÔËÐгÌÐò£¬ÎÒÃÇ´ò¿ªÊý¾ÝÁ¬½Ó£¬¿ØÖÆ̨´òÓ¡ÈçÏ£º

\


Èý¡¢µçÁ¿±ä»¯

Èç¹ûÎÒÃÇÔĶÁÈí¼þ£¬¿ÉÄÜÊÇÈ«ÆÁÔĶÁ£¬Õâ¸öʱºòÓû§¾Í¿´²»µ½Ê£ÓàµÄµçÁ¿£¬ÎÒÃǾͿÉÒÔΪËûÃÇÌṩµçÁ¿µÄÐÅÏ¢¡£ÒªÏë×öµ½ÕâÒ»µã£¬ÎÒÃÇÐèÒª½ÓÊÕÒ»ÌõµçÁ¿±ä»¯µÄ¹ã²¥£¬È»ºó»ñÈ¡°Ù·Ö±ÈÐÅÏ¢£¬ÎÒÃǾÍÀ´ÊµÏÖÒÔÏ£º

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;
	}
}

±¾½²¾Íµ½ÕâÀTake your time and enjoy it


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved