简体   繁体   中英

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. The service is activated through SMS . Modification is required as the app doesn't works on Android v4.1 and above. I have seen an app on PlayStore named 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 . 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. 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. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM