Всем здорово! Не получается реализовать получение sms от одного номера, который задается в настройках. вот код:
MainActivity:package ru.apiscale.apiscale;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
SharedPreferences mSettings;
public static final String APP_PREFERENCES_NAME = "phone_number";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "19.07.2018 04:19:55", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String address = extras.getString("address");
String message = extras.getString("message");
TextView addressField = (TextView) findViewById(R.id.address);
TextView messageField = (TextView) findViewById(R.id.message);
addressField.setText(address);
messageField.setText(message);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
switch (id) {
case R.id.nav_manage:
startActivity(new Intent(this, ManageActivity.class));
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_coin) {
Intent intent = new Intent(MainActivity.this, CoinActivity.class);
startActivity(intent);
return true;
}
if (id == R.id.action_about) {
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
}
if (id == R.id.action_developers) {
Intent intent = new Intent(MainActivity.this, DevelopersActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_main) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
} else if (id == R.id.nav_graphic) {
Intent intent = new Intent(MainActivity.this, GraphicActivity.class);
startActivity(intent);
} else if (id == R.id.nav_manage) {
Intent intent = new Intent(MainActivity.this, ManageActivity.class);
startActivity(intent);
} else if (id == R.id.nav_send) {
Intent intent = new Intent(MainActivity.this, SendActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
SMSReceiver:package ru.apiscale.apiscale;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
public class SMSReceiver extends BroadcastReceiver {
SharedPreferences mSettings;
public static final String APP_PREFERENCES_NAME = "phone_number";
@Override
public void onReceive(Context context, Intent intent) {
if(mSettings.contains(APP_PREFERENCES_NAME)) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
String address = messages[i].getOriginatingAddress();
if (mSettings.getString(APP_PREFERENCES_NAME, "").contains("+79962394105")) {
Intent newintent = new Intent(context, MainActivity.class);
newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newintent.putExtra("address", address);
newintent.putExtra("message",
messages[i].getDisplayMessageBody());
context.startActivity(newintent);
} else {
Intent newintent = new Intent(context, MainActivity.class);
newintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newintent.putExtra("address", "ошибка");
newintent.putExtra("message", "ошибка");
context.startActivity(newintent);
}
}
}
}
}
Помогите пожалуйста!