Monday, April 23, 2012

Reading logcat content in android

2 steps to do this.

1. Edit your manifest file. and mention permission android.permission.READ_LOGS


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.readlogcatTest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_LOGS" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".ReadLogcatActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



2. Create Activity and put following code in onCreate() method or where you want



package com.readlogcatTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ReadLogcatActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
 
            StringBuilder log=new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                              log.append(line);
            }
            
            TextView tv = (TextView)findViewById(R.id.logcat);
            tv.setText(log.toString());
            bufferedReader.close();
            process.destroy();
          } catch (IOException e) {e.printStackTrace();
          }
    }
}




Thursday, April 19, 2012

Starting service at boot time in android


3 steps to do this

1. Create a Broadcast receiver to receive boot complete state

package com.outsms.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
 
public class StartAtBootServiceReceiver extends BroadcastReceiver 
{
 @Override
 public void onReceive(Context context, Intent intent) 
 {
  if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
   Intent i = new Intent();
   i.setAction("com.outsms.test.StartAtBootService");
   context.startService(i);
  }
 }
}

2. Create a service to be started from BroadCast Receiver


package com.outsms.test;

import android.app.Service;
import android.os.IBinder;
public class OutGoingSMSTestService extends Service {
  
    @Override
    public void onCreate() {
        /*
             Do whatever you want when Srevice first created before started
 */ 
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
     /*
 DO WHATEVER you want when service is started
 */
    }
    @Override
    public void onDestroy() {
     
 /*
        do whatever you want when Service destroyed
    */
       super.onDestroy();
    }
    
 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
 }
    
}


3. Modify your AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.outsms.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <service android:name=".OutGoingSMSTestService">
 <intent-filter>
  <action android:name="com.outsms.test.StartAtBootService">
  </action>
 </intent-filter>
</service>

      <receiver android:name=".StartAtBootServiceReceiver">
 <intent-filter>
  <action android:name="android.intent.action.BOOT_COMPLETED">
  </action>
  <category android:name="android.intent.category.HOME">
  </category>
 </intent-filter>
</receiver>      
  
    </application>
</manifest>


start debug it should work fine.

Creating HTTP Connection and seinding data to server in android

//this is JSON String to put your information inside it
String postData = "{\"request\":{\"type\":\"locationinfo\"},\"userinfo\":{\"latitude\":\""+latitude+"\",\"longitude\":\""+longitude+"\",\"deviceid\":\""+deviceid+"\"}}";


HttpClient httpClient = new DefaultHttpClient();
// Post method to send data to server
HttpPost post = new HttpPost();


post.setURI(new URI("http://myserver.com/myphppage.php"));

// set your post data inside post method    
post.setEntity(new StringEntity(postData));

// execute post request here 
HttpResponse response = httpClient.execute(post);



Tuesday, April 17, 2012

Intercept outgoing call and end call in android

 4 Steps to do this


1. Download android-src.jar API and rference it in your Project.


2. Now create package com.android.internal.telephony (exact same name of package) in your paroject's src folder.

and create interface in this package as following :
package com.android.internal.telephony;

public interface ITelephony {


boolean endCall();


void answerRingingCall();


void silenceRinger();

}

3. Create BroadCastReceiver in your project's package com.mydomain.testoutgoingcalls (your choice for packagename)

package com.mydomain.testoutgoingcalls;

import java.lang.reflect.Method;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;
public class CallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();

if(null == bundle)
return;

String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);


String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;

Toast.makeText(context, info, Toast.LENGTH_LONG).show();



TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
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) {
System.out.println("error on end call : "+e.getMessage());
e.printStackTrace();
}


}
}






4. Edit your AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mydomain.testoutgoingcalls"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".CallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
Now start debug and test it will work fine.