我正在開發一個接收消息並更改配置文件的Android應用程序。該應用程序有一個Activity
和一個Broadcast Receiver
。問題是,該應用程序工作完全正常爲API
水平直到17年,但之後Broadcast Receiver
不 似乎工作。我提到了我的MainActivity
和BroadCast Receiver
以及gradle
文件的代碼。Android應用程序無法在API級別工作> 17
MainActivity.java
public class MainActivity extends AppCompatActivity {
public EditText setPass;
public Button submit;
public static String password;
//Context context;
SharedPreferences sharedPreferences;
boolean b=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//context=getApplicationContext();
setPass=(EditText)findViewById(R.id.setPassEditId);
submit=(Button)findViewById(R.id.submitButtonId);
sharedPreferences= getApplicationContext().getSharedPreferences("Pritom",Context.MODE_PRIVATE);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
password = setPass.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("password", password);
editor.commit();
Toast.makeText(getApplicationContext(), "Password saved successfully" + password, Toast.LENGTH_LONG).show();
} catch (Exception e){
e.printStackTrace();
}
//b=sharedPreferences.contains("password")?true:false;
//Toast.makeText(getApplicationContext(),"b:"+b,Toast.LENGTH_LONG).show();
sharedPreferences=getApplicationContext().getSharedPreferences("Pritom",Context.MODE_PRIVATE);
Map<String,?> keys=sharedPreferences.getAll();
for(Map.Entry<String,?> entry:keys.entrySet()){
Toast.makeText(getApplicationContext(),entry.getKey()+":"+entry.getValue().toString(),Toast.LENGTH_LONG).show();
}
}
});
}
}
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
public AudioManager audioManager;
public String me = "";
public String last = "";
public static final String SMS_BUNDLE = "pdus";
public SharedPreferences sharedPreferences;
public static final String MyPREFERENCES = "MyPrefs";
boolean b = false;
@Override
public void onReceive(Context context, Intent intent) {
try {
sharedPreferences = context.getApplicationContext().getSharedPreferences("Pritom", context.MODE_PRIVATE);
b = sharedPreferences.contains("password") ? true : false;
if (b) {
Toast.makeText(context.getApplicationContext(), "Present", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context.getApplicationContext(), "Not present", Toast.LENGTH_LONG).show();
}
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
} catch (Exception e){
e.printStackTrace();
}
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
String pass3 = sharedPreferences.getString("password", null);
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
//str += "n";
String smsBody = msgs[i].getMessageBody().toString();
Log.d("Message",smsBody+pass3);
Toast.makeText(context,"pass:"+pass3,Toast.LENGTH_LONG).show();
if (smsBody.equals("@general" + pass3)) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(audioManager.RINGER_MODE_NORMAL);
}
}
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
}
}
}
...
}
在receiver
類的問題是,沒有Toast
消息越來越顯示API level>17(Jelly Bean)
但工作完全正常在API level<=17
構建:gradle這個(模塊:APP)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.shaloin.profile2"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
的AndroidManifest.xml
`
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
`
是API 17+的時候嗎?或23+?因爲在23+有運行時權限。 –
你如何發送廣播?你可以發佈代碼嗎? –
我正在使用默認的'消息傳遞'應用程序發送消息。我已經發布了'broadcast receiver'類的代碼。 –