【问题标题】:Call blocking for a particular time period in AndroidAndroid中特定时间段的呼叫阻止
【发布时间】:2014-08-25 18:32:40
【问题描述】:

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

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

 }

【问题讨论】:

    标签: java android timer phone-call


    【解决方案1】:

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

    这是一个简单的方法:-

    • 从主活动中获取用户的开始时间和结束时间,并将其存储在Shared Preferences中。

    • <intent-filter> <action android:name="android.intent.action.PHONE_STATE"></action> </intent-filter>注册您的接收方

    • 在您的 Receiver 类中从 Shared Preferences 中获取存储的值,将其与当前时间进行比较 [就像您在 Pervasive_2Activity 中所做的那样],如果满足条件,则结束调用(块call) 否则什么都不做。

    现在可以在所需的设定时间段之间阻止呼叫。

    【讨论】:

      【解决方案2】:

      我对如何做到这一点的看法:

      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();
         }
      
         }
      

      【讨论】:

      • 你能告诉我,我如何在活动中从用户那里获取时间,以及如何通过系统服务时间获取该活动的时间来检查......或者没有需要创建活动.....
      • 您必须从活动中收集服务应该运行的持续时间,为此使用 timepicker。第一个 timepicker 应该给你开始时间,第二个 timepicker 会给你结束时间。转换为毫秒并传递给服务
      • 我很难理解你的代码......注册方法在做什么,我可以在哪里放置我的阻塞代码,我如何以及在哪里可以编写我的时间检查器代码,我该如何调用它我在时间检查器代码中的阻塞代码......请你能给我更多的代码......
      • 您的阻塞不是已经在 PhoneCallReceiver1 中了。 registerreceiver 函数将为您启动它。我已经在我的回答中更新了 intentfilter 代码。
      • 它在下一行显示错误...此行有多个标记..... phonecallReceiver = new PhonecallReceiver(cal.getTimeInMillis(), getApplicationContext()); .....错误- phonecallReceiver 无法解析为变量... - cal 无法解析 .... 它是 phonecallreciever 或 PhoneCallReceiver1 ......我必须把我的时间检查代码放在哪里......
      猜你喜欢
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      相关资源
      最近更新 更多