gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:37 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com