短信唤醒应用程序无法正常工作

时间:2012-04-08 17:07:30

标签: android android-activity sms broadcastreceiver

我正在尝试创建一个应用程序,以便我的手机在收到短信时醒来。我试过这个。这是代码:

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();
        }
    }
}

这个应用程序做了什么?

  1. 在消息音之前几秒钟唤醒我的手机。
  2. 打开一个黑色的空白屏幕,标题为“WakeMeUp”。
  3. 现在,我想要的是:

    1. 唤醒手机,但几乎同时发出消息音。
    2. 我不希望在调用活动时打开空白屏幕。

1 个答案:

答案 0 :(得分:1)

  1. 延迟可能是由您自己的代码造成的。尝试从广播接收器中删除耗时的代码,看看延迟是否减少。

  2. 如果您不想要空白屏幕,只需不要使用活动。如果要在后台运行某些内容,请使用服务。