im beginner on android app developer... spend almost two day on modifier and search solution for this kind of checkbox value store into sharedpreference...but failed. gt any other method to rewrite this coding so checkbox value can be store in sharedpreference. Hope expert can hlp...thk `
CheckBox blockAll_cb;
BroadcastReceiver CallBlocker;
TelephonyManager telephonyManager;
ITelephony telephonyService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
initviews();
blockAll_cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CallBlocker =new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Class c = null;
try {
c = Class.forName(telephonyManager.getClass().getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method m = null;
try {
m = c.getDeclaredMethod("getITelephony");
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
m.setAccessible(true);
try {
telephonyService = (ITelephony)m.invoke(telephonyManager);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
}
PhoneStateListener callBlockListener = new PhoneStateListener()
{
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING)
{
if(blockAll_cb.isChecked())
{
try {
telephonyService.endCall();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
};
};//BroadcastReceiver
IntentFilter filter= new IntentFilter("android.intent.action.PHONE_STATE");
registerReceiver(CallBlocker, filter);
}
});
}
public void initviews()
{
blockAll_cb=(CheckBox)findViewById(R.id.cbBlockAll);
//blockcontacts_cb=(CheckBox)findViewById(R.id.cbBlockContacts);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (CallBlocker != null)
{
unregisterReceiver(CallBlocker);
CallBlocker = null;
}
}
}
Aucun commentaire:
Enregistrer un commentaire