[英]Android: How to override default behavior of dialer
I am making modifications in a previous project, which is basically developed for kids and teenagers. 我正在以前的项目中进行修改,该项目基本上是为儿童和青少年开发的。 It blocks
text messaging, emails, calls and internet
when a user drives at speed greater than 16 MPH. 当用户以超过16 MPH的速度行驶时
text messaging, emails, calls and internet
它会阻止text messaging, emails, calls and internet
。 The service is activated through SMS
. 该服务通过
SMS
激活。 Modification is required as the app doesn't works on Android v4.1 and above. 由于该应用程序无法在Android v4.1及更高版本上运行,因此需要进行修改。 I have seen an app on
PlayStore
named Safely Go . 我在
PlayStore
上看到过一个名为Safely Go的应用。 This app has a button on click of which, status changes to driving. 该应用程序具有单击按钮,状态更改为行驶。 In this mode, if a user clicks on dialer, messaging, settings or browser, their own activity opens up, instead of default behavior.
在这种模式下,如果用户单击拨号程序,消息传递,设置或浏览器,则会打开他们自己的活动,而不是默认行为。 In short, in this mode a user cannot access anything except those applications, which the user has chosen to use during driving.
简而言之,在这种模式下,用户无法访问除在驾驶过程中选择使用的那些应用程序以外的任何内容。
As per my requirement, the service will be activated through SMS
. 根据我的要求,该服务将通过
SMS
激活。 Once service gets activated, and user's speed reaches more than 16 MPH, I want that when user clicks on Dialer
, my activity should open which will show a warning message, instead of opening dial pad. 一旦激活服务,并且用户的速度达到16 MPH以上,我希望当用户单击
Dialer
,我的活动应该打开,该活动将显示警告消息,而不是打开拨号盘。 I am not getting how can I accomplish this feature. 我不知道如何完成此功能。 As a service has no direct interaction with user, so the android system cannot catch key event in service.
由于服务与用户没有直接交互,因此android系统无法捕获服务中的关键事件。 Or, is there any other way to do this.
或者,还有其他方法可以做到这一点。
I simply want to know how can I override default behavior dialer. 我只想知道如何覆盖默认行为拨号程序。 If someone has any information please share, as it would be of great help for me.
如果有人有任何信息,请分享,因为这对我有很大帮助。
In the app that I mentioned in my question no default behavior was overridden. 在我提到的应用程序中,没有默认行为被覆盖。 That was my mistake, I took it in wrong way.
那是我的错误,我以错误的方式认错了。 The same functionality can be achieved using Handler .
使用Handler可以实现相同的功能。 In my app I used a service which starts on click of a button.
在我的应用程序中,我使用了一项服务 ,该服务从单击按钮开始。 Inside the service,, I had used handler.
在服务内部,我使用过处理程序。 Below, I am posting the code snippet.
在下面,我发布了代码片段。
public class DialerService extends Service {
ActivityManager am;
List<RunningAppProcessInfo> mAppProcessInfosList;
private Runnable myRunnable;
boolean threadDone = true;
Handler mHandler;
boolean isLockedAppRunning = false;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate() {
am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
mAppProcessInfosList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
mHandler = new Handler();
Log.v("Dialer Service", "onCreate called");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
myRunnable = new Runnable() {
@Override
public void run() {
isRestrictedAppRunning();
}
};
new Thread(new Runnable() {
public void run() {
while (threadDone) {
try {
mHandler.post(myRunnable);
} catch (Exception e) {
}
}
}
}).start();
return START_STICKY;
}
private void isRestrictedAppRunning() {
mAppProcessInfosList = am.getRunningAppProcesses();
for (int i = 0; i < mAppProcessInfosList.size(); i++) {
if (mAppProcessInfosList.get(i).processName
.equals("com.android.phone")
|| mAppProcessInfosList.get(i).processName
.equals("com.android.email")
|| mAppProcessInfosList.get(i).processName
.equals("com.android.mms")) {
isLockedAppRunning = true;
Intent dialogIntent = new Intent(getBaseContext(),
TestActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
this.threadDone = false;
}
}
This code is from my dummy app. 这段代码来自我的虚拟应用程序。 There are lot more conditions in actual app and still lots of things need to implemented.
实际应用中还有很多条件,仍然需要执行很多事情。 But, what I needed actually will work in this way.
但是,实际上我需要的将以这种方式工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.