繁体   English   中英

如何在来电时解锁手机?

[英]How to unlock the phone on incoming call?

我正在制作一个项目,其中我使用透明屏幕,并在来电屏幕上显示按钮。 如果手机处于解锁但手机处于锁定状态并且通话电话到手机上,那么它工作正常我首先必须解锁手机

我用的代码是......

public class Recevo extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
        Intent i= new Intent(context,Servo.class);
        int state = tm.getCallState();

      if(state==TelephonyManager.CALL_STATE_RINGING){

            String number=intent.getStringExtra("incoming_number");
            Log.i("State", "Call is Ringing");



            context.startService(i);

        }else if (state==TelephonyManager.CALL_STATE_IDLE) {

            Log.i("State", "Idle State");
            Toast.makeText(context, "Call is Idle", Toast.LENGTH_SHORT).show();
            context.stopService(i);

        } else if (state==TelephonyManager.CALL_STATE_OFFHOOK) {


            //Log.i("State", "Call is Ringing");
            //Toast.makeText(context, "Phone is Ringing", Toast.LENGTH_SHORT).show();

        }

    }

}


public class Servo extends Service{

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        //Toast.makeText(getApplicationContext(), "in servo", Toast.LENGTH_SHORT).show();


        Thread t=new Thread(new Times());
        t.start();
    }

    public class Times extends Thread{
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                Thread.sleep(5000);
                Intent i = new Intent(Servo.this,Blank.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

public class Blank extends Activity implements OnClickListener{

    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blank);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
        AudioManager audio1 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        //audio1.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        audio1.setStreamMute(AudioManager.STREAM_MUSIC, true);
        finish();
    }

更新1

public class CallerService extends Service implements
TextToSpeech.OnInitListener {

String number;

boolean a;
private TextToSpeech tts;
int callState;
TelephonyManager mgr;


@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();

tts = new TextToSpeech(this, this);
mgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
AudioManager audio1 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

int maxVolume = audio1.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float percent = 2.0f;
int seventyVolume = (int) (maxVolume*percent);
audio1.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);

Thread t=new Thread(new Times());
t.start();

}

public class Times extends Thread{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            Thread.sleep(5000);
            Intent i = new Intent(CallerService.this,Blank.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);

number = intent.getStringExtra("phnumber");


//fetchContacts();
}

@Override
public void onInit(int status) {
// TODO Auto-generated method stub
 if (status != TextToSpeech.ERROR) 
 {
         tts.setLanguage(Locale.ENGLISH);
         fetchContacts();
 } 
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub

if (tts != null) {
    tts.stop();
    tts.shutdown();
}
super.onDestroy();

}



public void fetchContacts() {

/*  tts.speak("Hello everyone", TextToSpeech.QUEUE_FLUSH,
        null);*/

String phoneNumber = null;

Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

String _ID = ContactsContract.Contacts._ID;

String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;

String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;

String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

ContentResolver contentResolver = getContentResolver();

Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null,
        null);

if (cursor.getCount() > 0) {

    while (cursor.moveToNext()) {

        String contact_id = cursor
                .getString(cursor.getColumnIndex(_ID));

        int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor
                .getColumnIndex(HAS_PHONE_NUMBER)));

        if (hasPhoneNumber > 0) {

            Cursor phoneCursor = contentResolver.query(
                    PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",
                    new String[] { contact_id }, null);

            while (phoneCursor.moveToNext()) {

                phoneNumber = phoneCursor.getString(phoneCursor
                        .getColumnIndex(NUMBER));

                a = PhoneNumberUtils.compare(number,
                        phoneNumber);
                callState= mgr.getCallState();

              /*if(!a){             

                    if(callState==TelephonyManager.CALL_STATE_RINGING) {

                    for (int i = 0; i < 15; i++) {

                        callState = mgr.getCallState();

                        if (callState==TelephonyManager.CALL_STATE_IDLE){
                            tts.stop();
                            tts.shutdown();
                            break;                  

                            }else if (callState==TelephonyManager.CALL_STATE_OFFHOOK){
                                tts.stop();
                                tts.shutdown(); 
                            break;  
                            }               

                        tts.speak("Unknown Calling", TextToSpeech.QUEUE_FLUSH,
                                null);
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
                }                   

                else */if (a) {

                    String contname = phoneCursor.getString(phoneCursor
                            .getColumnIndex(DISPLAY_NAME));

                    if (!contname.equals(null)) {


                        Toast.makeText(getApplicationContext(),
                                contname, Toast.LENGTH_SHORT).show();


                        if(callState==TelephonyManager.CALL_STATE_RINGING) {

                        for (int i = 0; i < 15; i++) {

                            callState = mgr.getCallState();


                            if (callState==TelephonyManager.CALL_STATE_IDLE){
                                tts.stop();
                                tts.shutdown();
                                break;                  

                                }else if (callState==TelephonyManager.CALL_STATE_OFFHOOK){
                                    tts.stop();
                                    tts.shutdown(); 
                                break;  
                                }               

                            tts.speak(contname + " Calling", TextToSpeech.QUEUE_FLUSH,
                                    null);
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    }
                    }
                }

            }

            phoneCursor.close();

        }


    }

}
if(!a){             

    if(callState==TelephonyManager.CALL_STATE_RINGING) {

    for (int i = 0; i < 15; i++) {

        callState = mgr.getCallState();

        if (callState==TelephonyManager.CALL_STATE_IDLE){
            tts.stop();
            tts.shutdown();
            break;                  

            }else if (callState==TelephonyManager.CALL_STATE_OFFHOOK){
                tts.stop();
                tts.shutdown(); 
            break;  
            }               

        tts.speak("Unknown Calling", TextToSpeech.QUEUE_FLUSH,
                null);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
}       
}

}

在Blank类中

public class Blank extends Activity implements OnClickListener{

    Button b;
    Window window;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blank);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(this);

        window = this.getWindow();

        // use Window.addFlags() to add the flags in WindowManager.LayoutParams
        window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
        AudioManager audio1 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        //audio1.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        audio1.setStreamMute(AudioManager.STREAM_MUSIC, true);
        finish();
    }


}

试试以下:

private Window window;

@Override
protected void onResume() {    
   super.onResume();

   // get the window of your activity
   window = this.getWindow();

   // use Window.addFlags() to add the flags in WindowManager.LayoutParams
   window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
   window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
   window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

}

注意:您也可以将此代码放在onCreate()中,但我建议将其放在onResume().

FLAG_DISMISS_KEYGUARD - 设置窗口时,只有当它不是安全锁定键盘锁时才会解除键盘锁定。

FLAG_SHOW_WHEN_LOCKED - 屏幕锁定时显示窗口的特殊标志

FLAG_TURN_SCREEN_ON - 当设置为添加或显示窗口时,一旦显示窗口,系统将激活电源管理器的用户活动(就像用户已唤醒设备一样)以打开屏幕。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM