2016-07-12 10 views
2

Hier habe ich Android-Projekt, die eingehenden Anruf Telefonnummer erfassen können. In PhoneStateReceiver.java Ich habe String namens eingehende Nummer. Ich brauche den Wert von incomingNumber zu Textview numberView in activity_main.xml genannt zu drucken.Nehmen Sie Zeichenfolge Wert zu Textveiw von Anthere Java-Klasse

PhoneStateReceiver.java

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.widget.TextView; 
import android.widget.Toast; 

public class PhoneStateReceiver extends BroadcastReceiver 
{ 

    public void onReceive(Context context, Intent intent) { 
     try { 

      String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
      String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 

      if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
       Toast.makeText(context,"Ringing State Number is - " + incomingNumber, Toast.LENGTH_SHORT).show(); 
      } 

     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 
    }  
} 

activity_main.xml

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" " 
     android:id="@+id/numberView" 
    /> 

Und unten zeigen meine anderen manifest.xml & MainActivity.java Dateien für Ihre Referenz.

manifest.xml

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     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> 

     //integrating Broadcast Receiver to our project. 
     <receiver android:name=".PhoneStateReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.PHONE_STATE" /> 
      </intent-filter> 
     </receiver> 
    </application> 

    //take permission from the users 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
</manifest> 

MainActivity.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 


public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


    } 
} 

Bitte helfen Sie mir, mein Problem zu lösen.

+0

müssen Sie BroadcastReceiver für bestimmte Aktivität erstellen. – comeback4you

+0

ja ich bereits BroadcastReceiver auf PhoneStateReceiver.java Datei wie oben gezeigt erstellen –

Antwort

1
public class MainActivity extends AppCompatActivity { 
TextView numberView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    numberView = (TextView) findViewById(R.id.numberView); 
    registerReceiver(tripMessageReceiver, new IntentFilter("msg")); 

} 

private final BroadcastReceiver tripMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     WakeLocker.acquire(context); 
     Log.i("no", getIntent().getStringExtra("no")); 
numberView.setText(getIntent().getStringExtra("no")); 
     WakeLocker.release(); 
    } 
}; 

@Override 
public void onDestroy() { 
    try { 
     unregisterReceiver(tripMessageReceiver); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    super.onDestroy(); 
} 
} 



public class PhoneStateReceiver extends BroadcastReceiver 
{ 

public void onReceive(Context context, Intent intent) { 
    try { 

     String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
     String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 

     if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
Intent intentBroadCast; 
      intentBroadCast = new Intent("msg"); 
      intentBroadCast.putExtra("no", incomingNumber); 
      context.sendBroadcast(intentBroadCast); 
      Toast.makeText(context,"Ringing State Number is - " + incomingNumber, Toast.LENGTH_SHORT).show(); 
     } 

    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 
}  
} 

public abstract class WakeLocker { 
private static PowerManager.WakeLock wakeLock; 

@SuppressLint("Wakelock") 
@SuppressWarnings("deprecation") 
public static void acquire(Context context) { 
    if (wakeLock != null) 
     wakeLock.release(); 

    PowerManager pm = (PowerManager) context 
      .getSystemService(Context.POWER_SERVICE); 
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK 
      | PowerManager.ACQUIRE_CAUSES_WAKEUP 
      | PowerManager.ON_AFTER_RELEASE, "WakeLock"); 
    wakeLock.acquire(); 
} 

public static void release() { 
    if (wakeLock != null) 
     wakeLock.release(); 
    wakeLock = null; 
}} 

<uses-permission android:name="android.permission.WAKE_LOCK" />//add permission in menifest file 

BroadcastReceiver für bestimmte Aktivität

Verwandte Themen