判別國家sim卡mnc、mcc號並自動安裝的應用

         

國際移動用戶識別碼(IMSI) International Mobile Subscriber Identity

國際上爲唯一識別一個移動用戶所分配的號碼。

從技術上講,IMSI可以徹底解決國際漫遊問題。但是由於北美目前仍有大量的AMPS系統使用MIN號碼,且北美的MDN和MIN採用相同的編號,系統已 經無法更改,所以目前國際漫遊暫時還是以MIN爲主。其中以O和1打頭的MIN資源稱爲IRM(International Roaming MIN),由IFAST (International Forum on ANSI-41 Standards Technology)統一管理。目前聯通申請的IRM資源以09打頭。可以看出,隨着用戶的增長,用於國際漫遊的MIN資源將很快耗盡,全球統一採用 IMSI標識用戶勢在必行.

IMSI共有15位,其結構如下:

MCC+MNC+MIN

MCC:Mobile Country Code,移動國家碼,共3位,中國爲460;

MNC:Mobile Network Code,移動網絡碼,共2位,聯通CDMA系統使用03,一個典型的IMSI號碼爲460030912121001;

MIN共有10位,其結構如下:

09+M0M1M2M3+ABCD

其中的M0M1M2M3和MDN號碼中的H0H1H2H3可存在對應關係,ABCD四位爲自由分配。

可以看出IMSI在MIN號碼前加了MCC,可以區別出每個用戶的來自的國家,因此可以實現國際漫遊。在同一個國家內,如果有多個CDMA運營商,可以通過MNC來進行區別.
public void fetch_status(){  
     TelephonyManager tm = (TelephonyManager) 
     this.getSystemService(Context.TELEPHONY_SERVICE);//      
     String str = "";  
     str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";    
     str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";    
     str += "Line1Number = " + tm.getLine1Number() + "\n";    
     str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";    
     str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";    
     str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";    
     str += "NetworkType = " + tm.getNetworkType() + "\n";    
     str += "PhoneType = " + tm.getPhoneType() + "\n";    
     str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";    
     str += "SimOperator = " + tm.getSimOperator() + "\n";    
     str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";    
     str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";    
     str += "SimState = " + tm.getSimState() + "\n";    
     str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";    
     str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";    
     TextView sys = (TextView) findViewById(R.id.sys);  
     sys.setText(str);  
}  

 

 

                                                                                                                                                                                                                                                                                                                                                             

package hyz.com;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
public class CardTestActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Uri uri = Uri.parse("http://www.baidu.com/");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }
}
package hyz.com;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class BootCompleteReceiver extends BroadcastReceiver 
{	
	public void onReceive(Context arg0, Intent arg1) 
	{
		String action = arg1.getAction();
		if(action.equals(Intent.ACTION_BOOT_COMPLETED))
		{
			Bundle args = new Bundle();
			args.putInt(AppService.OPCODE, AppService.OP_BOOT_COMPLETED);
			arg0.startService(new Intent(arg0 ,AppService.class).putExtras(args));
			
		}
	}
}

package hyz.com;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.TelephonyIntents;
import com.android.internal.telephony.IccCard;
public class AppService extends Service 
{
	static final String OPCODE = "op";
	static final int OP_BOOT_COMPLETED = 0;
	String imsi;
	private Context mContext = null;
	private final BroadcastReceiver mReceiver = new StkBroadcastReceiver();
public void onCreate()
{
	mContext = getBaseContext();
	IntentFilter intentFilter = 
			new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
	registerReceiver(mReceiver,intentFilter);
}
	@Override
	public void onStart(Intent intent, int startId) 
	{
		if(intent == null)
			return;
		mContext = getBaseContext();//實例化
		try
		{
			Bundle args = intent.getExtras();
			if(args == null)
				return ;
			if(args.getInt(OPCODE) == OP_BOOT_COMPLETED)
			{
				AppInstaller.unInstall(mContext);
			}
			IntentFilter intentFilter = new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
			registerReceiver(mReceiver, intentFilter);
		}catch(Exception e)
		{
			
		}		
	}
	private class StkBroadcastReceiver extends BroadcastReceiver
	{

		@Override
		public void onReceive(Context context, Intent intent) 
		{
			// TODO Auto-generated method stub
			String action = intent.getAction();
			TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			if(action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED))
			{
				android.util.Log.i("",intent.getStringExtra(IccCard.INTENT_KEY_ICC_STATE)+IccCard.INTENT_VALUE_ICC_IMSI);
				
				if(intent.getStringExtra(IccCard.INTENT_KEY_ICC_STATE).equals(IccCard.INTENT_VALUE_ICC_LOADED))
				{
					if(tm.getSubscriberId() != null)
					{
						String MCC = "";
						String MNC = "";
						imsi = tm.getSubscriberId();
						if(imsi.length() >= 5)
						{
							MCC = imsi.substring(0, 3);
							MNC = imsi.substring(3, 5);
						}
						if(MCC.equals("734")&&MNC.equals("04"))
						{
							AppInstaller.install(mContext);
						}
					}
				}
			}
			
		}
		
	}
	@Override
	public IBinder onBind(Intent arg0)
	{		
		return null;
	}
	public void onDestroy()
	{
		unregisterReceiver(mReceiver);
	}

}

package hyz.com;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
public class AppInstaller 
{
	private AppInstaller() {}
	static void install(Context context)
	{	
		setAppState(context , true);
	}
	static void unInstall(Context context)
	{
		setAppState(context , false);
	}
	private static void setAppState(Context context, boolean install)
	{
		if(context == null)
		{
			return ;
		}
		PackageManager pm = context.getPackageManager();
		if(pm == null)
		{
			return ;
		}
		ComponentName cName = new ComponentName("hyz.com", "hyz.com.CardTestActivity");
		int state = install ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
		try
		{
			pm.setComponentEnabledSetting(cName, state, PackageManager.DONT_KILL_APP);
		}
		catch(Exception e){}		
	}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hyz.com"
    android:versionCode="1"
    android:versionName="1.0" >     
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:clearTaskOnLaunch="true" >
        <activity
            android:label="@string/app_name"
            android:name=".CardTestActivity"
            android:enabled="false" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver 
            android:name=".BootCompleteReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>                
        </receiver>
        <service android:name="AppService"></service>
    </application>
</manifest>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章