我正在尝试制作一个应用程序,以便我的手机在收到短信时唤醒。我试着做了这个。这是代码:
package com.atiffarrukh.wakemeup;
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 SmsReceiver extends BroadcastReceiver
{
boolean received = false;
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
received = true;
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Intent i = new Intent(context,WakeUp.class); //for starting activity from broadcast
i.putExtra("ReceivedCheck", received);//put value of received
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//flags the intent to start activity
context.startActivity(i);
}
}
}
活动是:
package com.atiffarrukh.wakemeup;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
public class WakeUp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = getIntent();
boolean check = intent.getBooleanExtra("ReceivedCheck", false);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//Toast.makeText(getBaseContext(), "This is WAKEUP Act", Toast.LENGTH_SHORT).show();
boolean isScreenOn = pm.isScreenOn();
if(check && !isScreenOn ){
/*getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show();
*/
//Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show();
final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My tag");
wl.acquire();
Thread timer = new Thread(){
public void run(){
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO: handle exception
}finally{
wl.release();
}
}
};
timer.start();
}
}
}
这个应用程序有什么作用?
- 在消息音前几秒钟唤醒我的手机。
- 打开以“WakeMeUp”为标题的空白黑屏。
现在,我想要的是:
- 用消息音唤醒手机,但几乎同时。
- 我不希望在调用活动时打开那个空白屏幕。