package com.lockscreen;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class MyCallReceiver extends BroadcastReceiver {
static boolean isRinging=false;
static boolean isReceived=false;
static String callerPhoneNumber;
static boolean phonestatus=false;
static DbHelper mDbHelper;
static SQLiteDatabase mDb;
public static String Tell;
Cursor nCursor;
Main ms=new Main();
@Override
public void onReceive(Context mContext, Intent intent){
intent.getExtras();
// Get current phone state
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state==null)
return;
//phone is ringing
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
isRinging =true;
//get caller's number
Bundle bundle = intent.getExtras();
callerPhoneNumber= bundle.getString("incoming_number");
}
//phone is received
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
isReceived=true;
}
// phone is idle
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
// detect missed call
if(isRinging==true && isReceived==false) {
Toast.makeText(mContext, "Got a missed call from : " + callerPhoneNumber, Toast.LENGTH_LONG).show();
}
isRinging = false;
isReceived = false;
ms.Searcher();
if(ms.Tel.equals(callerPhoneNumber)){
ms.updater(true);
SendtoSMS();
Intent TestIntent= new Intent(mContext,LockScreenAppActivity.class).putExtra("aaa", "ABCDEFG");
PendingIntent pendIntent = PendingIntent.getActivity(mContext, 0, TestIntent, PendingIntent.FLAG_ONE_SHOT);
TestIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(TestIntent);
}
else{
Log.d("TAG",callerPhoneNumber+" ");
}
}
}
public void SendtoSMS(){
String msage="왜얌";
SmsManager mSmsManager = SmsManager.getDefault();
mSmsManager.sendTextMessage(ms.Tel, null, msage, null, null); //SMS발송
}
}
여기까지가 리시버받는 클래스 고
여기가 메인 부분인데
package com.lockscreen;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
class DbHelper extends SQLiteOpenHelper{ //DBHelper 클래스 재정의
public DbHelper (Context context){
super(context, "inside", null, 1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table Mypage (" +
"_id integer primary key autoincrement, " +
"name text, " +
"tel text, "+"lock int, "+"grade int);");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("drop table if exists Mypage");
onCreate(db);
}
}
public class Main extends Activity {
public static DbHelper mDbHelper;
public static SQLiteDatabase mDb;
static Cursor mCursor;
static String name=null;
static String Tel;
int st,gr;
EditText edn;
EditText edt;
int count=0;
public static String strQuery;
public static boolean lockstatus=false; //true일때 잠금이 필요한 상태
public static boolean grade=true; //true가 부모 false 자식
TextView tv;
public static int ccc=0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//여기가 진짜 메인 필요한거 1. DB 2. 나누기
//입력 부분 eidtor1 = name editor2= Tel They say They don't know us anyway watch burn let it die cause we are finally free tonight
final EditText edn=(EditText)findViewById(R.id.edit1);// 네임
final EditText edt =(EditText)findViewById(R.id.edit2);
Button bt=(Button)findViewById(R.id.bu);
Button bt2=(Button)findViewById(R.id.bu2);
final TextView tv=(TextView)findViewById(R.id.tv1);
mDbHelper = new DbHelper(this);
mDb = mDbHelper.getWritableDatabase();
Searcher();
tv.setText(Tel);
bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
/*Intent intent = new Intent(getApplicationContext(), LockScreenAppActivity.class);
startActivity(intent);
*/
name=edn.getText().toString();
Tel=edt.getText().toString();
if(count==0){
Insert();
}
else{
updaterph(Tel);
}
Searcher();
}
});
bt2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
/*Intent intent = new Intent(getApplicationContext(), LockScreenAppActivity.class);
startActivity(intent);
*/
Searcher();
Intent intent2 = new Intent(getApplicationContext(), LockScreenAppActivity.class);
startActivity(intent2);
finish();
}
});
}
public void Searcher(){
strQuery = "SELECT * FROM Mypage";
mDbHelper = new DbHelper(this);
mDb = mDbHelper.getWritableDatabase();
mCursor = mDb.rawQuery(strQuery, null);
for (int i = 0; i < mCursor.getCount(); i++) {
mCursor.moveToNext();
ccc=mCursor.getInt(0);
Tel = mCursor.getString(2);
st=mCursor.getInt(3);
gr=mCursor.getInt(4);
count++;
Log.d("TAG",Tel+" "+st+" "+gr+" "+ccc);
}
if(st!=0){
lockstatus=true;
}
if(gr!=0){
grade=true;
}
}
public void updaterph(String ph){
mDb.execSQL("UPDATE Mypage SET tel = '"+ph+"' where _id = "+1 );
}
public void updater(boolean fr){
int r;
if(fr==true){
r=1;
}
else
{
r=0;
}
mDb.execSQL("UPDATE Mypage SET lock = "+r+" where _id = "+1 );
}
public void Insert(){
int grader=1;
int locker=0;
try {
mDb.execSQL("INSERT INTO Mypage VALUES(null, '"+name+"','"+Tel+"','"+locker+"','"+grader+"');");
} catch (Exception e) {
Log.d("common","장비를정지합니당>.<");
}
}
}
리시버에서 먼저 전화가 와서 부재중 전화가 된 다음 디비 탐색을 해야 하는데 탐색 함수 호출을 하면
mCursor = mDb.rawQuery(strQuery, null);에서 널 포인트 익셉션이 떠요..