Reputation: 52810
I am developing an android application for teenagers, they can not do/receive any incoming/outgoing calls/text messages during driving and also they can change status either he/she is driving or not. In my application I want to handle both situations Block/Unblock incoming/outgoing calls/text messages. If you have any idea related to this question than please share with me.
Thanks.
Upvotes: 2
Views: 13031
Reputation: 52810
After 8 Hours R&D I found my Own Solution for my above question:
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deactivateimservicedemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- For Incoming Text messages -->
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.deactivateimservicedemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.deactivateimservicedemo.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/btnActivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activate" />
<Button
android:id="@+id/btnDeactivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Deactivate" />
</LinearLayout>
MainActivity.java
package com.example.deactivateimservicedemo;
import java.lang.reflect.Method;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.android.internal.telephony.ITelephony;
public class MainActivity extends ActionBarActivity {
private Button btnActivate, btnDeactivate;
private BroadcastReceiver receiver;
private IntentFilter filter;
private boolean isReceiverRegistered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnActivate = (Button) findViewById(R.id.btnActivate);
btnActivate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/*Register Receiver*/
registerReceiver(receiver, filter);
isReceiverRegistered = true;
}
});
btnDeactivate = (Button) findViewById(R.id.btnDeactivate);
btnDeactivate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*UnRegister Receiver*/
if(isReceiverRegistered){
Log.i("Receiver is", "Register");
unregisterReceiver(receiver);
}else {
Log.i("Receiver is", "Not Register");
}
}
});
filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Intent Action", action);
if(intent!=null){
if(action.equals("android.intent.action.PHONE_STATE")){
try {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m
.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
Log.e("Exception", e.toString());
}
}else if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle extras = intent.getExtras();
if (extras != null) {
abortBroadcast();
}
}
}
}
};
}
@Override
protected void onDestroy() {
btnDeactivate.performClick();
super.onDestroy();
}
}
That's IT.
Upvotes: 1
Reputation: 5867
Use the new Activity API to detect when user is in driving mode.
To block incoming call you will have to listen for TelephonyManager.CALL_STATE_RINGING events and kill the incoming call. See code example here
I'm not sure what you mean by blocking incoming SMS. Do you want to delete arriving SMSs while user is driving (bad idea)? If so - read this one. This tecqnique should work for at least Android < 4.4. Remember to assign highest priority possibly to your SMS receiver.
Upvotes: 1
Reputation: 8106
There are several attempts. Use GooglePlayServices Drive Api https://developer.android.com/google/play-services/drive.html
Then you can check the speed. If this is higher then 6km/h you may want to register an telephony and SMS BroadcastReceiver.
With the Receivers you can handle the incoming stuff. This is how it should be done.
Another attempt is checking GPS Position with a PositionChance of example 10 metres, calculate the time between the location-jumps and if it's less then 2sec for 10 metres you can enable this too, using an AlarmManager which blocks the stuff for at least 30 minutes.
Another attempts is checking the CellIDs (position) and validating how often it is changed to a new CellID (position).
http://www.devx.com/wireless/Article/40524 Is a good example how to get the Location using CellIDs
Upvotes: 0