目前我正在开发一个呼叫阻止应用。在完成应用程序后,我发现了一个错误: - 假设我阻止了有号码的人A的联系" 9806584545"但是当A人打电话给我时,他的号码显示的是ISD代码,而且数字不是阻止的。因此,要阻止人员A的电话号码,我需要保存他与ISD代码的联系,然后阻止该号码,然后才能使用。请有人帮助我。以下是我的代码: -
BlockedContactAdapter.java: -
import java.util.ArrayList;
import com.rsoft.callblocker.R;
import com.rsoft.notificationCenter.CallBlockerToastNotification;
import com.rsoft.objects.BlockedContact;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class BlockedContactAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<BlockedContact>contacts;
public BlockedContactAdapter(Activity activity, ArrayList<BlockedContact>contacts)
{
this.activity=activity;
this.contacts=contacts;
}
@Override
public int getCount()
{
return contacts.size();
}
@Override
public Object getItem(int position)
{
return contacts.get(position);
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.blacklist_layout, null);
}
final BlockedContact cn=contacts.get(position);
TextView name=(TextView)vi.findViewById(R.id.blacklistNameTxt);
TextView number=(TextView)vi.findViewById(R.id.blacklistNumberTxt);
name.setText(cn.getName());
number.setText(cn.getNumber());
/*if(cn.getNumber().length() == 10) {
num = country_code.concat(cn.getNumber());
} else if (cn.getNumber().length() <= 10) {
Toast.makeText(activity, "Invalid number", Toast.LENGTH_SHORT).show();
} else {
num = cn.getNumber();
}
number.setText(num);*/
final ImageButton callButton=(ImageButton)vi.findViewById(R.id.blacklistCallButton);
final ImageButton smsButton=(ImageButton)vi.findViewById(R.id.blacklistSmsButton);
final ImageButton deleteButton=(ImageButton)vi.findViewById(R.id.blacklistDeleteButton);
if(cn.isBlockedForCalling())
{
callButton.setImageResource(R.drawable.phonered);
}
if(cn.isBlockedForMessages())
{
smsButton.setImageResource(R.drawable.smsred);
}
callButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(cn.isBlockedForCalling() )
{
cn.setBlockedForCalling(false);
callButton.setImageResource(R.drawable.phoneblack);
CallBlockerToastNotification.showDefaultShortNotification(cn.getName() + " can call you");
}
else
{
cn.setBlockedForCalling(true);
callButton.setImageResource(R.drawable.phonered);
CallBlockerToastNotification.showDefaultShortNotification(cn.getName()+" can not call you");
}
}
});
smsButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(cn.isBlockedForMessages())
{
cn.setBlockedForMessages(false);
smsButton.setImageResource(R.drawable.smsblack);
CallBlockerToastNotification.showDefaultShortNotification(cn.getName()+" can text you");
}
else
{
cn.setBlockedForMessages(true);
smsButton.setImageResource(R.drawable.smsred);
CallBlockerToastNotification.showDefaultShortNotification(cn.getName()+" can not text you");
}
}
});
deleteButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
AlertDialog dialog=new AlertDialog.Builder(v.getContext()).create();
dialog.setTitle("Delete contact");
dialog.setMessage("Are you sure you want to delete this contact from blacklist?");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes" ,new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
CallBlockerMainActivity.blackList.remove(cn.getNumber());
CallBlockerBlacklistViewActivity.updateListData();
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE,"No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
dialog.setCancelable(false);
dialog.setIcon(R.drawable.advert);
dialog.show();
}
});
return vi;
}
}
DeviceStateListener: -
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.android.internal.telephony.ITelephony;
import com.rsoft.callBlockerService.CallBlockerService;
import com.rsoft.objects.BlockedContact;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class DeviceStateListener extends PhoneStateListener
{
private ITelephony telephonyService;
private Context context;
public DeviceStateListener(Context context)
{
this.context=context;
initializeTelephonyService();
}
private void initializeTelephonyService()
{
try
{
TelephonyManager telephonyManager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Class clase=Class.forName(telephonyManager.getClass().getName());
Method method=clase.getDeclaredMethod("getITelephony");
method.setAccessible(true);
telephonyService=(ITelephony)method.invoke(telephonyManager);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void onCallStateChanged(int state, final String incomingNumber)
{
switch(state)
{
case TelephonyManager.CALL_STATE_RINGING:
final BlockedContact cn=CallBlockerService.blackList.get(incomingNumber);
if(cn!=null && cn.isBlockedForCalling())
{
try
{
telephonyService.endCall();
Thread t=new Thread(new Runnable()
{
@Override
public void run()
{
DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd - HH:mm:ss");
Date date=new Date();
String currentDate=dateFormat.format(date);
//LOG FORMAT --> TITLE;;MESSAGE;;NAME;;NUMBER;;HOUR;;BODYMESSAGE(NULL);;SEPARATOR
String message="Call Blocked;;A call from "+cn.getName()+" ("+incomingNumber+") was blocked at "+currentDate+";;"+cn.getName()+";;"+incomingNumber+";;"+currentDate+";;NULL;;\r\n";
writeInLog(message);
}
});
t.start();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
break;
}
}
public void writeInLog(String message)
{
try
{
OutputStreamWriter fos = new OutputStreamWriter(context.openFileOutput("CallLog.txt", Context.MODE_APPEND));
fos.append(message);
fos.close();
System.out.println("Writed in log succesfully");
}
catch(Exception e)
{
Log.e("Error", e.getMessage());
}
}
}