Please tell me if there is an application that receives messages (including in the background (i.e. when it is closed or minimized)). On the smartphone with the version android 5.1.1, the application works fine, i.e. Messages in the background arrive, but on the android version 7.1 is not. Do you need to register a request to the user, provide the opportunity for the application to work in the background, if so how to do it (specifically permission for the application to work in the background)? If not, what could be the solutions to this problem?
Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.danil.smssecurity"> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Acrans.Acr.SplashActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Acrans.Acr.acran0_1" /> <activity android:name=".Acrans.Acr.acran1" /> <activity android:name=".Acrans.Acr.acran2" /> <activity android:name=".Acrans.Acr.acran3" /> <activity android:name=".Acrans.Acr.acran4" /> <activity android:name=".Acrans.Acr.acran5" /> <activity android:name=".Acrans.Acr.acran6" /> <activity android:name=".Acrans.Acr.acran7" /> <activity android:name=".Acrans.Acr.MainActivity" /> <activity android:name=".Acrans.Acr.acran8" /> <activity android:name=".Acrans.Acr.acran2_2" /> <receiver android:name=".Acrans.ForSms.MessageReceiver"> <intent-filter android:priority="100"> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> <activity android:name=".Acrans.Acr.acran4_1"></activity> </application> </manifest> MessageReceiver.java
public class MessageReceiver extends BroadcastReceiver { private static final String TAG="myLogs"; private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; DBHelper dbHelper; public static String writeNameNumer []; com.example.danil.smssecurity.Acrans.Acr.acran1 acran1=new acran1(); @Override public void onReceive(Context context, Intent intent) { dbHelper=new DBHelper(context); SQLiteDatabase database = dbHelper.getWritableDatabase(); ContentValues contentValues = new ContentValues(); Bundle bundle = intent.getExtras(); SmsMessage[] messages; String str = ""; if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); messages = new SmsMessage[pdus != null ? pdus.length : 0]; String phone=""; String message=""; for (int i = 0; i < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) (pdus != null ? pdus[i] : null)); //1-адресс приходс сообщения str += messages[i].getOriginatingAddress(); str += ": "; //2-само сообщение str += messages[i].getMessageBody(); str += "\n"; phone=messages[i].getOriginatingAddress(); message=messages[i].getMessageBody(); Cursor cur = database.query(DBHelper.TABLE_MESSAGE, new String[] {"_id", DBHelper.MESS_PHONE}, null, new String[] {}, null, null, null); boolean k=cur.moveToFirst(); if (k==false) { Log.i(TAG, "база пуста"); contentValues.put(DBHelper.MESS_PHONE, phone); contentValues.put(DBHelper.MESS_MESSAGE, message); contentValues.put(DBHelper.MESS_MARCER, "me"); database.insert(DBHelper.TABLE_MESSAGE, null, contentValues); Intent broadcastIntent = new Intent(); broadcastIntent.setAction("SMS_RECEIVED_ACTION"); broadcastIntent.putExtra("message", str); context.sendBroadcast(broadcastIntent); } else { Log.i(TAG, "база не пуста"); // database.delete(DBHelper.TABLE_CONTACTS, DBHelper.KEY_ID, null); contentValues.put(DBHelper.MESS_PHONE, phone); contentValues.put(DBHelper.MESS_MESSAGE, message); contentValues.put(DBHelper.MESS_MARCER, "me"); database.insert(DBHelper.TABLE_MESSAGE, null, contentValues); Intent broadcastIntent = new Intent(); broadcastIntent.setAction("SMS_RECEIVED_ACTION"); broadcastIntent.putExtra("message", str); context.sendBroadcast(broadcastIntent); } String searchNomer=""; char one=phone.toCharArray()[0]; String sim= Character.toString(one); contentValues = new ContentValues(); if (sim.equals("+")){ searchNomer=phone.substring(2); } else { if ((sim.equals("7"))|(sim.equals("8"))) { searchNomer=phone.substring(1); } } int col=0; one=phone.toCharArray()[0]; sim= Character.toString(one); Zamena_nomera_na_name(searchNomer, sim, phone, context); if ((sim.equals("+"))|(sim.equals("7"))|(sim.equals("8"))){ if ((phone.length()>10)&(phone.length()<=12)){ Cursor zxcv = database.query(DBHelper.TABLE_NUMBER, new String[] {}, DBHelper.NUM_PHONE + " LIKE ?" , new String[] {"%"+searchNomer}, null, null, null); if (zxcv.moveToFirst()) { int countIndex=zxcv.getColumnIndex(DBHelper.NUM_COUNT); int phoneIndex = zxcv.getColumnIndex(DBHelper.NUM_PHONE); do { Log.i(TAG, "phone = " + zxcv.getString(phoneIndex)); try { col=Integer.parseInt(zxcv.getString(countIndex)); col++; } catch (NumberFormatException e) { col=1; } database.delete(DBHelper.TABLE_NUMBER, DBHelper.NUM_PHONE +" LIKE ?" , new String[] {"%"+searchNomer}); Log.i(TAG, "запись успешно удалена"); } while (zxcv.moveToNext()); } else { Log.i(TAG,"0 rows"); col=1; } zxcv.close(); contentValues = new ContentValues(); contentValues.put(DBHelper.NUM_PHONE, this.writeNameNumer[1]); contentValues.put(DBHelper.NUM_NAME, this.writeNameNumer[0]); contentValues.put(DBHelper.NUM_COUNT, col); database.insert(DBHelper.TABLE_NUMBER, null, contentValues); } } String KEY=""; Cursor rebol = database.query(DBHelper.TABLE_CONTACTS, new String[] {DBHelper.KEY_KLUCH}, DBHelper.KEY_PHONE + " LIKE ?" , new String[] {"%"+searchNomer}, null, null, null); if (rebol.moveToFirst()) { int keyInd = rebol.getColumnIndex(DBHelper.KEY_KLUCH); do { KEY =rebol.getString(keyInd); } while (rebol.moveToNext()); } else { contentValues = new ContentValues(); contentValues.put(DBHelper.KEY_PHONE, this.writeNameNumer[1]); contentValues.put(DBHelper.KEY_KLUCH, R.string.defaultKey); contentValues.put(DBHelper.KEY_NAME, this.writeNameNumer[0].toLowerCase()); database.insert(DBHelper.TABLE_CONTACTS, null, contentValues); Log.i(TAG,"0 rows");} rebol.close(); Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); } abortBroadcast(); } } // замена номера на имя при сохранение в поле editText public void Zamena_nomera_na_name(String searchNomer, String sim, String phone, Context context) { Log.i(TAG, "Message Receiver замена номера на имя"); this.writeNameNumer=new String [2]; dbHelper=new DBHelper(context); SQLiteDatabase database = dbHelper.getWritableDatabase(); try { if ((sim.equals("+"))|(sim.equals("7"))|(sim.equals("8"))){ if ((phone.length()>10)&(phone.length()<=12)){ Cursor zxcv = database.query(DBHelper.TABLE_CONTACTS, new String[] {}, DBHelper.KEY_PHONE + " LIKE ?" , new String[] {"%"+searchNomer}, null, null, null); if (zxcv.moveToFirst()) { int phoneIndex = zxcv.getColumnIndex(DBHelper.KEY_PHONE); int nameIndex=zxcv.getColumnIndex(DBHelper.KEY_NAME); do { this.writeNameNumer[0]=zxcv.getString(nameIndex); this.writeNameNumer[1]=zxcv.getString(phoneIndex); } while (zxcv.moveToNext()); } else { Log.i(TAG,"0 rows"); } zxcv.close(); } } } catch (Exception e) { Log.i(TAG,"Message Receiver ошибка при замене номера на имя"); } } }