Thursday, 26 February 2015

Parse Notification in android

Step 1. Create account in parse and get application id and client id.

Step. Copy parse lib in your libs folder

Step 3.Create class

Application.java

public class Application extends android.app.Application {

public Application() {
}

@Override
public void onCreate() {
super.onCreate();

// Initialize the Parse SDK.
Parse.initialize(this, "ThKJesxP8ZCB5VGziCHeOEWaJjy406MRQ96stgCy",
"RGiIsDzDx3N4FH6NmgIF4z8DT7uzwQJphi79kDNF");

// Specify an Activity to handle all pushes by default.
PushService.setDefaultPushCallback(this, Home_Activity.class);
}
}


Step 4. Call these methods in your main activity

try {

PushService.subscribe(this, "Giants", Home_Activity.class);
PushService.setDefaultPushCallback(this, Home_Activity.class);
ParseAnalytics.trackAppOpened(getIntent());
} catch (Exception e) {
// TODO: handle exception
}


Step 5. Declaration in mainfest file

    <!-- Parse -->
   
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.******.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.******.permission.C2D_MESSAGE" />

// Call application class in your first activity

// android:name="com.*****.Application"


At last receiver need to declare in manifest

      <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <service android:name="com.parse.PushService" />

        <receiver android:name="com.parse.ParseBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.*****" />
            </intent-



Thats all hopefully somebody help this topic thanks....

Sunday, 15 February 2015

Track calls in android

Step 1. Create one receiver to receive your call and detect,

MyPhoneReceiver.class

public class MyPhoneReceiver extends BroadcastReceiver {

private static final String AUDIO_RECORDER_FILE_EXT_AMR = ".amr";

public static final String AUDIO_RECORDER_FOLDER = "CallRecords";

private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.AMR_NB };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_AMR };
Context context;

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
} else if (intent.getAction().equals(
"android.intent.action.PHONE_STATE")) {
handleCalls(context);

} else if (intent.getAction().equals(
"android.intent.action.NEW_OUTGOING_CALL")) {
handleOutgoingCalls(context, intent);
}
}

void sysAlert(String title, String msg, Context context) {
AlertDialog alert = new AlertDialog.Builder(context).setTitle(title)
.setMessage(msg)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.setCanceledOnTouchOutside(false);
alert.show();
}

public void onDestroy() {

telephony.listen(null, PhoneStateListener.LISTEN_NONE);
}

TelephonyManager telephony;

MyPhoneStateListener phoneListener;

void handleOutgoingCalls(Context context, Intent intent) {

String number = intent
.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

try {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
Log.e("testing", "outgoing call Recording ringing");

} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.e("testing", "outgoing call Recording OffHook");
try {
startRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
}

} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Log.e("testing", "outgoing  call Recording idle");
try {
stopRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
}

}
} catch (Exception e) {
e.printStackTrace();
}

}

void handleCalls(Context context) {
if (phoneListener == null) {
phoneListener = new MyPhoneStateListener(context);
telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);

}
}

public class MyPhoneStateListener extends PhoneStateListener {

Context context;

MyPhoneStateListener(Context context) {
this.context = context;
}

boolean ring = false;
boolean callReceived = false;

public void onCallStateChanged(int state, String incomingNumber) {
Log.e("Testing", "" + state + ", " + incomingNumber);
switch (state) {

case TelephonyManager.CALL_STATE_RINGING:
ring = true;
// Database db = new Database(context);
// db.insertRecord(incomingNumber, "", Call_details.typeCall);
// db.close();
context.sendBroadcast(new Intent("onNewCall"));
Log.e("testing", "Incoming call Recording ringing");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callReceived = true;
Log.e("testing", "Incoming call Recording OffHook");
// phoneRinging = true;
// phoneRinging = false;
try {
startRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
}
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.e("testing", "Incoming call Recording idle");
try {
stopRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
}
if (ring == true && callReceived == false) {
Toast.makeText(context,
"It was A MISSED CALL from : " + incomingNumber,
Toast.LENGTH_LONG).show();
}

break;
}

}
}

private String getFilename() {

String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);

if (!file.exists()) {
file.mkdirs();
}

return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}

private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);

try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void stopRecording() {
// if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
// }
}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
}
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
}
};

}


--------------------------------------------------------------------------------------------------------------------
Step 2: 

Main class,

MainActivity.class

public class MainActivity extends Activity {

Context context;
Activity act;
ListView lvcall;
public static final String typeCall = "2";
public static final String typeOutgoingCall = "3";

BroadcastReceiver onNewCall = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

}
};
BroadcastReceiver onOutgoingCall = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
act = MainActivity.this;
registerReceiver(onNewCall, new IntentFilter("onNewCall"));
registerReceiver(onOutgoingCall, new IntentFilter("onOutgoingCall"));

lvcall = (ListView) findViewById(R.id.lvcall);
callno_record = (TextView)findViewById(R.id.callno_record);
calldetails(typeCall, typeOutgoingCall);
}

@Override
protected void onDestroy() {
super.onDestroy();
try {
unregisterReceiver(onNewCall);
unregisterReceiver(onOutgoingCall);
} catch (Exception e) {
e.printStackTrace();
Log.e("test", "crash in call details");
}

}
void calldetails(String type, String type1) {
Database db = new Database(act);
ArrayList<HashMap<String, String>> al = db.getRecord(type);

db.close();
for (int i = 0; i < al.size(); i++) {
HashMap<String, String> hm = al.get(i);
String name = getName(act.getContentResolver(),
hm.get(phoneNo));
// hm.put("name", hm.get(phoneNo) + " " + name);
hm.put("name", hm.get(phoneNo));
}
if (al.size() > 0) {
lvcall.setVisibility(View.VISIBLE);
SimpleAdapter adapter = null;

if (type.equals(typeCall)) {

adapter = new SimpleAdapter(act, al,
R.layout.call_details_view,
new String[] { "name", time }, new int[] {
R.id.txtPhoneNo, R.id.txtTime });

} else if (type1.equals(typeOutgoingCall)) {

adapter = new SimpleAdapter(act, al,
R.layout.call_details_view,
new String[] { "name", time }, new int[] {
R.id.txtPhoneNo, R.id.txtTime });
}
lvcall.setAdapter(adapter);
callno_record.setVisibility(View.GONE);
} else {
lvcall.setAdapter(null);
lvcall.setVisibility(View.GONE);
}

}

public String getName(ContentResolver cr, String phoneNo) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);

while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber = phoneNumber.trim().replaceAll(" ", "");
if (phoneNumber.equals(phoneNo))
return name;
}
phones.close();
return "UnKnown";
}

}

------------------------------------------------------------------------------------------------------------------------

Step 3.:
Allow  permissions in your manifest file,

this is your uses permission:
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_USER_DICTIONARY" />

and receiver is here,

 <receiver android:name="com.test.MyPhoneReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

--------------------------------------------------------------------------------------------
thats all, hope this is useful for somebody.. thanks