我有以下自定义侦听器
package home.dan.signalstrengthatlocation;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class CustomPhoneStateListener extends PhoneStateListener {
Context mContext;
public static String LOG_TAG = "CPhoneStateListener";
public CustomPhoneStateListener(Context context) {
mContext = context;
}
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
Log.i(LOG_TAG, "onSignalStrengthsChanged: " + signalStrength);
Intent i = new Intent("Signal_update");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("signals","signals updated");
CustomPhoneStateListener.this.mContext.startActivity(i);
//mContext.startActivity(i); also does not work
}
}
在最后一个命令中,它尝试将数据发送到MainActivity类,以便通过它的onReceive方法获取。
但这不起作用。我收到以下错误。
android.content.ActivityNotFoundException: No Activity found to handle Intent
将数据从上面的Listner类发送到MainActivity类的最佳方法是什么?
编辑 -
下面的课程应该接收来自上述意图的信息
package home.dan.signalstrengthatlocation;
import android.app.Activity;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final int NETWORKTYPE_WIFI = 0;
private static final int NETWORKTYPE_4G = 1;
private static final int NETWORKTYPE_2G = 2;
private static final int NETWORKTYPE_NONE = 3;
public TelephonyManager mTelephonyManager;
public PhoneStateListener mListener;
private Button start_btn,stop_btn;
private TextView textview;
private BroadcastReceiver broadcastReceiver;
private String coordinates;
public String getCoordinates() {
return coordinates;
}
public void setCoordinates(String coordinates) {
this.coordinates = coordinates;
}
private String signalStrength;
public String getSignalStrength() {
return signalStrength;
}
public void setSignalStrength(String signalStrength) {
this.signalStrength = signalStrength;
}
@Override
protected void onResume() {
super.onResume();
if(broadcastReceiver == null){
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
setCoordinates(intent.getExtras().get("coordinates").toString());
updateScrollText();
}
};
}
registerReceiver(broadcastReceiver,new IntentFilter("location_update"));
}
private void updateScrollText(){
textview.append("\n"+getCoordinates()+" "+getSignalStrength());
}
@Override
protected void onDestroy() {
super.onDestroy();
if(broadcastReceiver != null){
unregisterReceiver(broadcastReceiver);
}
}
GPS_Service gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.listen(new CustomPhoneStateListener(this), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
start_btn = (Button) findViewById(R.id.button1);
stop_btn = (Button) findViewById(R.id.button2);
textview = (TextView) findViewById(R.id.textview);
if(!runtime_permissions()){
enable_buttons();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the child Activity.
setSignalStrength(data.getStringExtra("signals"));
updateScrollText();
}
}
private void enable_buttons(){
start_btn.setEnabled(true);
stop_btn.setEnabled(false);
start_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),GPS_Service.class);
startService(i);
if(!stop_btn.isEnabled()){
start_btn.setEnabled(false);
stop_btn.setEnabled(true);
}
}
});
stop_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),GPS_Service.class);
stopService(i);
if(!start_btn.isEnabled()){
start_btn.setEnabled(true);
stop_btn.setEnabled(false);
}
}
});
}
private boolean runtime_permissions(){
if(Build.VERSION.SDK_INT >= 23
&&
ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&&
ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&&
ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_PHONE_STATE},
100);
return true;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 100){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
enable_buttons();
}else{
runtime_permissions();
}
}
}
}
答案 0 :(得分:1)
在MainActivity的onResume()中,添加以下内容
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("location_update");
intentFilter.addAction("signal_update);
registerReceiver(broadcastReceiver, intentFilter);
然后在CustomPhoneStateListener中使用sendBroadcast而不是startActivity。
Intent i = new Intent("signal_update");
i.putExtra("signals","signals updated");
CustomPhoneStateListener.this.mContext.sendBroadcast(i);
您将在此处同时收到信号和位置更新:
public void onReceive(Context context, Intent intent) {
if("signal_update".equals(intent.getAction())) {
//update UI for signal update
} else if ("location_update".equals(intent.getAction())) {
// update UI for location update
}
}