Displaying A Message From Broadcastreceiver
I wrote a simple class that extends BroadcastReceiver to handle an incoming call and get the caller's number. Now I want to display that number in a popup window or something simil
Solution 1:
I created a IncomingCallService
service that extends from BroadcastReceiver
to handle an incoming call and get the caller's number. Now I displayed that Incoming caller number in a popup window like TrueCaller. Its working fine me. Hope it should helpful for you. Please try and let me know. Thanks
CallReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
publicclassCallReceiverextendsBroadcastReceiver {
static String IncomingNumber;
@OverridepublicvoidonReceive(final Context context, Intent intent) {
TelephonyManagerteleMgr= (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListenerpsl=newPhoneStateListener() {
@OverridepublicvoidonCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
IncomingNumber = incomingNumber;
Log.i("CallReceiverBroadcast", "Incoming call caught. Caller's number is " + incomingNumber + ".");
Intenti=newIntent(context, IncomingCallService.class);
context.startService(i);
case TelephonyManager.CALL_STATE_IDLE:
Log.i("CallReceiverBroadcast", "CALL_STATE_IDLE");
IncomingCallService.clearView(context);
// Call Disconnectedbreak;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("CallReceiverBroadcast", "CALL_STATE_OFFHOOK");
IncomingCallService.clearView(context);
// Call Answer Modebreak;
}
}
};
teleMgr.listen(psl, PhoneStateListener.LISTEN_CALL_STATE);
teleMgr.listen(psl, PhoneStateListener.LISTEN_NONE);
}
}
IncomingCallService.java:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
publicclassIncomingCallServiceextendsService {
staticViewview=null;
static WindowManager wm;
static LayoutInflater inflater;
static WindowManager.LayoutParams params;
privatestatic TextView txtIncomingnumber;
staticbooleanprocessingAction=false;
@Overridepublic IBinder onBind(Intent intent) {
returnnull;
}
@OverridepublicintonStartCommand(Intent intent, int flags, int startId) {
super.onCreate();
params = newWindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, 350,
WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_main,null);
wm.addView(view, params);
txtIncomingnumber = (TextView)view.findViewById(R.id.txtIncomingnumber);
txtIncomingnumber.setText("You have Incoming Call from " + CallReceiver.IncomingNumber);
return START_STICKY;
}
publicstaticvoidclearView(Context context) {
wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
if(view != null) {
if(view.isEnabled()){
wm.removeView(view);
view = null;
}
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_alignParentLeft="true"android:background="#0090FF"android:padding="10dp"android:keepScreenOn="true" ><TextViewandroid:id="@+id/txtIncomingnumber"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="30dp"android:text="Incoming Call Number"android:textColor="#fff" /></RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.stackoverflow"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="18" /><uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/><uses-permissionandroid:name="android.permission.CALL_PHONE" /><uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW" ></uses-permission><uses-permissionandroid:name="android.permission.VIBRATE"></uses-permission><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><receiverandroid:name=".CallReceiver" ><intent-filterandroid:priority="99999"><actionandroid:name="android.intent.action.PHONE_STATE"/></intent-filter></receiver><activityandroid:name="com.stackoverflow.MainActivity"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><serviceandroid:name=".IncomingCallService"></service></application></manifest>
Solution 2:
If you want to show a dialog from inside your onReceive of the BroadcastReceiver, you have to start a transparent activity with an alert dialog and NEVER called setContentView(). The activity will have an transparent view and only the alert dialog will show..
Your Broadcast Receiver onReceive
@OverridepublicvoidonReceive(Context context, Intent intent) {
Log.i("CallReceiverBroadcast", "onReceive() is called. ");
TelephonyManagerteleMgr=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListenerpsl=newPhoneStateListener() {
@OverridepublicvoidonCallStateChanged(int state, String incomingNumber) {
Log.i("CallReceiverBroadcast", "onCallStateChanged() is called. ");
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.i("CallReceiverBroadcast", "Incoming call caught. Caller's number is " +incomingNumber + ".");
//start activity which has dialog
Intent i=newIntent(context,DialogActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
};
teleMgr.listen(psl, PhoneStateListener.LISTEN_CALL_STATE);
teleMgr.listen(psl, PhoneStateListener.LISTEN_NONE);
}
And your DiaLogActivity
publicclassDialogActivityextendsActivity
{
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//dont call setcontent view
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?").setCancelable(
false).setPositiveButton("Yes",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).setNegativeButton("No",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialogalert= builder.create();
alert.show();
}
}
Post a Comment for "Displaying A Message From Broadcastreceiver"