这里我有Android项目,可以捕获来电号码。 在PhoneStateReceiver.java中,我有一个名为incomingNumber的字符串。我需要将 incomingNumber 的值打印到activity_main.xml中名为 numberView 的TextView。
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"
/>
下面显示我的其他manifest.xml&amp; MainActivity.java文件供您参考。
的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);
}
}
答案 0 :(得分: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