在Android中调用特定时间段的阻止功能

时间:2014-08-25 18:32:41

标签: java android timer phone-call

我正在开发一个应用程序,它将阻止特定时间段内的所有呼叫,但我在执行此应用程序时遇到一些问题,它没有阻止任何呼叫。所以请告诉我我的代码中有什么问题,它是阻止调用但不是特定的时间..我发布我的代码。

public class Pervasive_2Activity extends Activity {
  /** Called when the activity is first created. */

   private PhoneCallReceiver1 action;
int hours;
int minutes;
int seconds;
int hour1;
int minutes1;
int hour2;
int minutes2;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    action= new PhoneCallReceiver1();
    Date dt = new Date();
     hours = dt.getHours();
     minutes = dt.getMinutes();
     seconds = dt.getSeconds();

    Button OK = (Button) findViewById(R.id.widget39);

    OK.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    TimePicker tp1 = (TimePicker)findViewById(R.id.timePicker1);
     hour1 = tp1.getCurrentHour();
     minutes1 =tp1.getCurrentMinute();
     TimePicker tp2 = (TimePicker)findViewById(R.id.timePicker2);
     hour2 = tp2.getCurrentHour();
     minutes2 =tp2.getCurrentMinute();
    }
    });



    if((hours>hour1 && hours<hour2)&& (minutes>minutes1 && minutes<minutes2))
 {
    Context context = this.getApplicationContext();
 Intent intent = this.getIntent();

action.onReceive(context, intent);
 }
   }
  }

类 - 2

   public class PhoneCallReceiver1 extends BroadcastReceiver {
Context context = null;
private static final String TAG = "Phone call";
private ITelephony telephonyService;

    @Override
   public void onReceive(Context context, Intent intent) {
    Log.v(TAG, "Receving....");
   TelephonyManager telephony = (TelephonyManager) 
    context.getSystemService(Context.TELEPHONY_SERVICE);  
     try {
  Class c = Class.forName(telephony.getClass().getName());
  Method m = c.getDeclaredMethod("getITelephony");
   m.setAccessible(true);
   telephonyService = (ITelephony) m.invoke(telephony);
 //  telephonyService.silenceRinger();
  telephonyService.endCall();
 } catch (Exception e) {
   e.printStackTrace();
  }

 }


}

类 - 3

public interface ITelephony {


  boolean endCall();


  void answerRingingCall();


void silenceRinger();


  void call(String number);

 }

3 个答案:

答案 0 :(得分:0)

尝试登录

try{

    // First block call here
    for(int i=0;i<10;i++){
    Thread.sleep(1000);
    }
   // after 10 seconds unblock call here
    }catch(Exception e){

    }

希望上面的代码可以帮助你...

答案 1 :(得分:0)

除特定时间段外,您的广播接收器类似乎每时都会阻止呼叫。

  

这是一个简单的方法: -

     
      
  • 从主要活动中获取开始时间&amp;来自用户的结束时间并将其存储在共享首选项

  • 中   
  • 使用<intent-filter> <action android:name="android.intent.action.PHONE_STATE"></action> </intent-filter>

  • 注册您的接收者   
  • 在Receiver类中获取共享首选项中存储的值,将其与当前时间进行比较[正如您在Pervasive_2Activity中所做的那样],如果条件满足则结束调用(块电话)其他什么都不做。

  •   
     

现在,呼叫将能够在所需的设定时间段之间被阻止。

答案 2 :(得分:0)

我对如何做的看法:

1. Create a service 2. Register the BroadcastReceiver in the Service. 3. unregister and destroyservice when the specified time expires using a timertask.

创建服务

它不是完整的代码只是部分代码。

  public class CallDetectService extends Service {

    BroadcastReceiver PhoneCallReceiver1;

    public CallDetectService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        min = intent.getIntExtra("min", 0);
        hour = intent.getIntExtra("hour", 0);  //get the data from the activity
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        phonecallReceiver = new PhonecallReceiver(cal.getTimeInMillis(), getApplicationContext());
        registerReceiver(PhoneCallReceiver1 , intentFilter);

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                stopSelf();     //if time expires the service will be stopped
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask, d);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(getApplicationContext(), "Service destroy called", Toast.LENGTH_LONG).show();
        Log.v("myapp", "stopped the service");
        if (PhoneCallReceiver1 != null) {
            unregisterReceiver(PhoneCallReceiver1 );
            Log.v("myapp", "Unregistered receiver");
        }
        super.onDestroy();
    }

}

致电服务

致电该服务并将您的来回时间传递到此处。

final Intent service = new Intent(getActivity().getApplicationContext(), PhoneCallReceiver1 .class);
 service.putExtra("min", min);
service.putExtra("hour", hour);
getActivity().startService(service);

在接收器中添加阻止代码

public class PhoneCallReceiver1 extends BroadcastReceiver {
Context context = null;
private static final String TAG = "Phone call";
private ITelephony telephonyService;
private Long endtime;
public PhoneCallReceiver1(long time, Context c){
endtime=time;
}
    @Override
    public void onReceive(Context context, Intent intent) {
    Log.v(TAG, "Receving....");
    TelephonyManager telephony = (TelephonyManager) 
    context.getSystemService(Context.TELEPHONY_SERVICE);  

    Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
    if(endtime<calendar.getTimeInMillis()){
      //you need to block your call, add your logic to block your call.
    }
   catch (Exception e) {
   e.printStackTrace();
   }

   }