2017-07-29 66 views
0

我有以下的自定義偵聽如何發送意圖從CustomListener Android中

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(); 
     } 

    } 
} 

}

+1

什麼是你signal_update。在意圖聲明中,您通常會傳遞上下文和活動名稱。 –

+0

啊哈 - 我錯了我猜 - 我試圖使用「signal_update」作爲過濾Intents的關鍵。 –

+1

因此,您的問題現在排序或您希望我進一步幫助您 –

回答

1

接收信息。在您的onResume()你的MainActivity,加入以下

IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction("location_update"); 
intentFilter.addAction("signal_update); 

registerReceiver(broadcastReceiver, intentFilter); 

然後在CustomPhoneStateListener中使用sendBroadcast而不是startActivi TY。

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 
    } 
} 
+0

嗨,感謝您的快速響應,我是Android Dev的新手, 我認爲「Signal_Update」是在BoardcastReceiver過濾Intent Broadcasts的關鍵。 –

+1

可以請您提供您的廣播接收器的代碼,以及它是靜態接收器(在清單中聲明)還是動態註冊的活動本身。 – Amit

+0

我在主帖中添加了該代碼作爲新編輯 –