Wednesday, May 25, 2016

TELEPHONY BROADCAST RECEIVER IN ANDROID

                                  TELEPHONY BROADCAST RECEIVERS IN ANDROID

 

INCOMING CALL BROADCAST RECEIVER:

 

package com.dat.spy.spyroid;

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

public class MyPhoneReceiver extends BroadcastReceiver {



    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.w("MY_DEBUG_TAG", state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String phoneNumber = extras
                        .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.w("MY_DEBUG_TAG", phoneNumber);
                Toast.makeText(context,"Call action no.."+phoneNumber,Toast.LENGTH_LONG).show();
            }
        }
    }
}

OUTGOING CALL BROADCAST RECEIVER:


package com.dat.spy.spyroid;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;



public class OutgoingCallReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {

        String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.d(OutgoingCallReceiver.class.getSimpleName(), intent.toString() + ", call to: " + phoneNumber);
        Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();

    }
}


MESSAGE RECEVING BROADCAST RECEIVER:

 

package com.dat.spy.spyroid;


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

public class SmsBroadcastReceiver extends BroadcastReceiver {

    public static final String SMS_BUNDLE = "pdus";

    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null)
        {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, smsMessageStr, Toast.LENGTH_LONG).show();

         
        }
    }
}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dat.spy.spyroid">
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SpyActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".SmsBroadcastReceiver" android:exported="true" >
        <intent-filter android:priority="999" >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
        </receiver>

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

        <receiver android:name=".OutgoingCallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>



    </application>

</manifest>




Wednesday, May 18, 2016

DYNAMIC SIMPLE TABLE BIND VIEW FROM SQLITE DATABASE IN ANDROID-(DISCONNECTED MODEL)


DYNAMIC SIMPLE TABLE BIND VIEW FROM SQLITE DATABASE IN ANDROID



ViewActivity.java

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class ViewActivity_dis extends ActionBarActivity {
String cols[] = { "Id""First Name","LastName"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_activity_dis);
showData();
}
public void showData()
{

List<String[]> list = new ArrayList<String[]>();
String selectQuery = "SELECT  * FROM repos";
//SQLiteDatabase db = Addattendance.db.getWritableDatabase();
Cursor cursor =MainActivity. db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String ar[] = { cursor.getString(0), cursor.getString(1),cursor.getString(2)};
list.add(ar);
while (cursor.moveToNext());
}
TableRow.LayoutParams params = new TableRow.LayoutParams(100, 30);
TableLayout table = new TableLayout(getApplicationContext());

TableRow row = new TableRow(this);
for (int i = 0; i < cols.length; i++) 
{
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(params);
tv.setText(cols[i]);
row.addView(tv);
}

table.addView(row);
for (int i = 0; i < list.size(); i++)
{

String r[] = list.get(i);
row = new TableRow(this);
for (int j = 0; j < r.length; j++) 
{
TextView tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText(r[j]);
row.addView(tv);
}
table.addView(row);
}
table.setOrientation(TableLayout.HORIZONTAL);
ScrollView scroll = new ScrollView(getApplicationContext());

scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                             LayoutParams.FILL_PARENT));
scroll.addView(table);
setContentView(scroll);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_activity_dis, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


ViewActivity.xml



<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.salesmanager.BillReportActivity" >

</ScrollView>

output: