2015-10-31 67 views
0

我是一個Android初學者,我寫了下面的代碼,代碼可以顯示手機吐司與API 10,但我不能顯示吐司,並運行onReceive在手機API 19.吐司沒有顯示簡單的短信廣播接收器

我已經搜查了互聯網,發現我應該添加國旗意圖與flag_include_stopped_pa​​ckages。我想這是我的問題的答案。

但是,如何將它添加到系統廣播?讚賞如果任何人都可以顯示合適的code.I找不到任何合適的代碼從互聯網上顯示這一點。謝謝!

SMS.java

public class SMS extends AppCompatActivity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

IncomingSms.java

public class IncomingSms extends BroadcastReceiver { 
    final SmsManager sms = SmsManager.getDefault(); 

public void onReceive(Context context,Intent intent) { 
    final Bundle bundle = intent.getExtras(); 
    try { 
     if (bundle != null) { 
      final Object[] pdusObj = (Object[]) bundle.get("pdus"); 
      for (int i = 0; i < pdusObj.length; i++) { 
       SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); 
       String message = currentMessage.getDisplayMessageBody(); 
       String senderNum = currentMessage.getDisplayOriginatingAddress(); 
       Log.i("SmsReceiver", senderNum + message); 
       Toast.makeText(context, 
         "send from " + senderNum + message, Toast.LENGTH_LONG).show(); 

      } 
     } 
    } catch (Exception e) { 
     Log.e("SmsReceiver", "Exception smsReceiver" + e); 
    } 
} 
} 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.security.security" > 

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

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".SMS" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
      <category android:name="android.intent.category.INFO"/> 
     </intent-filter> 
    </activity> 

    <receiver android:name=".IncomingSms"> 
     <intent-filter android:priority="999"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
      </intent-filter> 
    </receiver> 

</application> 

回答

0

首先通過擴展Android默認BroadcastReceiver類來創建自己的Receiver類。然後@OverrideonReceive()方法在你的班級。這個onReceive()方法將在新的短信發送到手機時被調用。以下是我的接收器類獲取傳入的短信通知。

//Here is your broadcast receiver class 
public class YourBroadcastReceiver extends BroadcastReceiver{ 
    private static final String TAG = "MyBroadCastReceiver"; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Log.i(TAG,"OnReceive ++>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 
      Bundle bndl = intent.getExtras(); 
      SmsMessage[] msg = null; 
      String str = "";   
      if (null != bndl) 
      { 
       //---retrieve the SMS message received--- 
       Object[] pdus = (Object[]) bndl.get("pdus"); 
       msg = new SmsMessage[pdus.length];   
       for (int i=0; i<msg.length; i++){ 
        msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);    
        str += "SMS From " + msg[i].getOriginatingAddress();     
        str += " :\r\n"; 
        str += msg[i].getMessageBody().toString(); 
        str += "\n"; 
       } 
       //---display incoming SMS as a Android Toast--- 
       Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
      } 
     } 
} 

在Android中有兩種不同的方式註冊broadcastreceiver。

  1. 將broadcastreceiver註冊到項目的清單文件中。如果您將使用此方法,那麼您無法控制廣播接收器的生命週期。這意味着除非卸載應用程序,否則應用程序將收到通知。

  2. 使用Android的Context.registerReceiver()方法註冊broadcastreceiver。通過使用這種方法,我們可以根據您的要求通過註冊和取消註冊廣播接收機來控制它的生命週期。要使用Android的背景下注冊的BroadcastReceiver visit this Answer

因此,讓我們註冊自己的接收器類清單文件進入SMS意圖過濾器。對於傳入的SMS示例,我的清單文件如下所示。

<uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="19" /> 

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

<receiver 
     android:name ="FULL_PACKAGE_NAME.YourBroadcastReceiver"> 
     <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 

就是這樣。一旦你安裝了應用程序,那時廣播接收器將自動註冊到Android操作系統,並且你會收到詳細的Android Toast通知。

獲取更多信息visit here