2014-05-13 27 views
0

我想通过短信打开和关闭屏幕。这里是我的代码下面我不知道什么是错误的,因为它根本不工作。帮助我找到错误。我也附上清单文件。提前感谢您。 我的Java文件:屏幕通过短信接收和关闭

public class MyReceiver extends BroadcastReceiver{ 

String sender; 
@Override 
public void onReceive(Context context, Intent intent) { 

    SmsMessage[] sms = null; 

    Bundle b = intent.getExtras(); 

    String str = " SMS From : "; 
    if (b != null) { 

     Object[] pdus = (Object[]) b.get("pdus"); 

     sms = new SmsMessage[pdus.length]; 

     for (int i = 0; i < sms.length; i++) { 

      sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 

      if (i == 0) { 
       str += sms[i].getOriginatingAddress(); 
       str += ":"+sms[i].getMessageBody().toString();   

       }else if (sms[i].getMessageBody().equals("D")) { 
        Intent in2= new Intent(Intent.ACTION_SCREEN_OFF); 
        in2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(in2); 

       }else if (sms[i].getMessageBody().equals("E")) { 
        Intent in3= new Intent(Intent.ACTION_SCREEN_ON); 
        in3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(in3); 

      } 
     } 
    } 
     Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
     Log.d("Receiving", str); 
    } 

} 
} 

MY清单文件:

<uses-permission android:name="android.permission.RECEIVE_SMS"/> 

    <receiver android:name=".MyReceiver"> 
     <intent-filter android:priority="100"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
      <action android:name="android.intent.action.SCREEN_OFF"/> 
      <action android:name="android.intent.action.SCREEN_ON"/> 
      </intent-filter> 

      </receiver> 
</application> 

+0

那么它没有收到短信或不打开/关闭屏幕? – cYrixmorten

+0

它不会在接收时打开/关闭屏幕,但几次尝试后甚至会停止接收短信,就像它应该在接收短信时显示敬酒一样。 – Disguise

+0

你在接收器内部做了太多的工作。所有你应该做的就是收到意图,并将控制权交给活动或服务来完成工作。 –

回答

0

好吧,首先,关于短信的接收,你可能想看看我的回答是:communication between two device using sms

这是接收我发现的短信的最可靠的方式。

注意,短信可能不是,如果向上你不希望出现你可能要考虑切换到从互联网推送通知,而不是设备Hide sms notifications with a broadcastreceiver in KitKat

的消息从奇巧较新的设备的最佳选择。这很容易使用,例如parse.com

关于打开和关闭屏幕我使用下面的一段代码。

private PowerManager.WakeLock wl; 
private PowerManager pm; 

public void screenWakeup(Context context, Activity activity) { 

    try { 
     pm = (PowerManager) context 
       .getSystemService(Activity.POWER_SERVICE); 
     // if (!pm.isScreenOn()) { 
     if (wl == null) { 
      wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP 
        | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Turios"); 
     } 
     if (!wl.isHeld()) { 
      wl.acquire(); 
     } 

     final Window win = activity.getWindow(); 
     win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
       | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
     win.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 
       | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     // } 

    } catch (Exception ex) { 
     Log.e(TAG, ex.getMessage()); 
    } 

} 

public void screenRelease(Activity activity) { 

    if (releaseWakeLock()) { 
     activity.getWindow().clearFlags(
       WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
         | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
         | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 
         | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    } 

} 

请注意,它需要访问一个活动,所以它不会工作,除非你的活动是活着的。我会建议做的是:

  1. 接收开启/关闭消息
  2. 转发送一个新的广播例如sendBroadcast(new Intent("INTENT_TURN_SCREEN_ON_OFF"))
  3. 注册为广播的监听你的活动中

    // in onCreate 
    wakeScreenReceiver = new WakeScreenReceiver(); 
    registerReceiver(wakeScreenReceiver, new IntentFilter("INTENT_TURN_SCREEN_ON_OFF")); 
    
    // in onDestroy 
    unregisterReceiver(wakeScreenReceiver); 
    
    // WakeScreenReceiver defined somewhere inside the Acitivity 
    public class WakeScreenReceiver extends BroadcastReceiver { 
        private static final String TAG = "WakeScreenReceiver"; 
    
        public RefreshModules() { 
        } 
    
        @Override 
        public void onReceive(Context context, final Intent intent) { 
          screenWakeup(MainActivity.this, MainActivity.this); 
          // or 
          screenRelease(MainActivity.this); 
        } 
    } 
    

这样的应用程序将永远只能尝试打开/关闭屏幕如果活动是活的,因为否则的广播也根本不被接收。

+0

这很好。但对我来说,理解为基本的学习者是非常复杂的。谢谢,我会尽力理解这一点。 @cYrixmorten – Disguise