java - Android 中特定时间段的调用阻止

标签 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);

 }

最佳答案

您的广播接收器类似乎每时每刻都在阻止调用,除了特定的时间段。

Here is an simple approach :-

  • From the main-activity get the start time & end time from the user and store it in the Shared Preferences.

  • Register your Receiver with <intent-filter> <action android:name="android.intent.action.PHONE_STATE"></action> </intent-filter>

  • Inside your Receiver class get the stored values from the Shared Preferences, compare it with the current time [As u did in Pervasive_2Activity], If condition is satisfied then end the call(block call) Else do nothing.

Now the Calls will be able to blocked between the desired set time periods.

关于java - Android 中特定时间段的调用阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25492235/

相关文章:

java - 在单行 SWT 文本小部件中设置选项卡大小

java - java中二维数组如何按字母顺序排序

java - Azure 通知中心 java.net.ConnectException : Connection timed out: no further information

android - 在 android 4.0.3 上 react native 。可能的?

java - 如果任务在另一个类中运行,则停止计时器任务

javascript - 如何使用 jasmine 测试使用 setInterval 的 Angular 服务?

java - 如何在java appium中获取完整的元素列表(包括滚动时可见的元素)?

java - 如何在多个包 ID(同一项目)Android 中使用 Firebase 远程配置

android - 以编程方式检测 "Let Google apps access your location"是否已禁用

Delphi - 如何制作具有启动/停止功能的毫秒或纳秒计时器?