我在寻找例子。
我的班级看起来像:
<pre><code>
package com.example.pjimnez.samsung_auto_reply;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends AppCompatActivity {
//IntentFilter screenStateFilter;
Button btnStart;
TextView txtHome;
//PhoneState oPhoneState;
IntentFilter filter1;
private final BroadcastReceiver myPhoneState = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state == null) {
//Outgoing call
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, number, Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
//Log.e("tag", "EXTRA_STATE_OFFHOOK");
Toast.makeText(context, "EXTRA_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
//Log.e("tag", "EXTRA_STATE_IDLE");
Toast.makeText(context, "EXTRA_STATE_IDLE", Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
//Log.e("tag", "Incoming number : " + number);
Toast.makeText(context, number, Toast.LENGTH_LONG).show();
} else
Toast.makeText(context, "none", Toast.LENGTH_LONG).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filter1 = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
registerReceiver(this.myPhoneState, filter1);
btnStart = (Button) findViewById(R.id.btnStart);// Instancia del objeto boton intro
//No funciona en modo escucha
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtHome = (TextView) findViewById(R.id.txtHome);
txtHome.setText("Dio click en el boton!" );//cambio el contenido del TextView
//Envia el mensahe SMS
/*
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("5524234613", null, "Mensaje de prueba", null, null);
*/
}
});
}
}
enter code here
</code></pre>
我的清单有权:
android.permission.READ_PHONE_STATE
android.permission.RECEIVE_SMS
android.permission.SEND_SMS
问题是我的应用程序从不在电话更改状态下显示消息
我不知道BroadcastReceiver
有什么问题你能帮忙吗
答案 0 :(得分:-1)
试试这个:
1-添加一个类PhoneStateBroadcastReceiver,它扩展BroadcastReceiver并覆盖onReceive()
2-在运行时,请事先请求PHONE_STATE权限
ActivityCompat.requestPermissions(myMainActivity,
new String[]{Manifest.permission.READ_PHONE_STATE},
READ_PHONE_STATE_CODE);
并通过系统对话框
给出3-拨打电话
你会看到onReceive()中的意图:
intent:Intent {act = android.intent.action.READ_PHONE_STATE flg = 0x10 cmp = com.myApp.network.PhoneStateBroadcastReceiver(has extras)} 行动:android.intent.action.PHONE_STATE
希望有所帮助