2016-08-26 7 views
0

受信SMS用のブロードキャストレシーバを作成します。それはアプリケーションが開いている場合のみ動作します。タスクマネージャからアプリケーションを削除すると、そのアプリケーションは動作しません。それはゼリービーン、kitkatでうまく動作しますが、ロリポップバージョンでは動作しません。アプリケーションがロリポップのタスクマネージャから閉じるときにブロードキャストレシーバが動作しない

public class MobiricReceiver extends BroadcastReceiver { 

    // Get the object of SmsManager 
    final SmsManager sms = SmsManager.getDefault(); 

    public void onReceive(Context context, Intent intent) { 

     // Retrieves a map of extended data from the 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 phoneNumber = currentMessage.getDisplayOriginatingAddress(); 

        String senderNum = phoneNumber; 
        String message = currentMessage.getDisplayMessageBody(); 

        Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message); 


        // Show Alert 
        int duration = Toast.LENGTH_LONG; 
        Toast toast = Toast.makeText(context, 
          "senderNum: "+ senderNum + ", message: " + message, duration); 
        toast.show(); 

       } // end for loop 
      } // bundle is null 

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

     } 
    } 
} 

DecalarationでMenifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="com.example.admin.broadcast_demo1"> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <receiver android:name=".MobiricReceiver" 
      > 
      <intent-filter 
       android:priority="1"> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
       <action android:name="android.intent.action.USER_PRESENT" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </receiver> 

    </application> 

</manifest> 
+1

あなたはどのデバイスをテストしていますか? – earthw0rmjim

+0

タスクマネージャからアプリを終了してもBroadcastRecieverは動作している必要がありますが、この問題は 'android.provider.Telephony.SMS_RECEIVED'アクションで発生する可能性があります。どのデバイスをテストしていますか? – Kushal

+0

私はこれに対する解決策を得ました。シンプルにこれをアクティビティに入れます。 android:autoRemoveFromRecents = "true"。アプリケーションは決して殺しません。私はロリポップ5.1でテストする –

答えて

0

それはMenifestでsolution.Addこれを非常に簡単です - >ボタンのバックプレスでアクティビティタグ

 <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:autoRemoveFromRecents="true"> 
     </activity> 

ください。このコードでアクティビティを終了します

if(android.os.Build.VERSION.SDK_INT >= 21) 
          { 
           getActivity().finishAndRemoveTask(); 
          } 
          else 
          { 
           getActivity().finish(); 
          } 
関連する問題