making an activity with a transparent view over incoming call in android - android

I want to show a sort of popup on the incoming call screen. this is my code, basically i'm getting the phone number, making another thread and showing it in the log.
public class CustomBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "CustomBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
// Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Log.v(TAG, "phoneNr: "+phoneNr);
TestAsyncTask myATask = new TestAsyncTask();
myATask.execute("one", "two", "three", "four");
//someActivity some = new someActivity();
}
I was told i would need to handle.Delay() my method for about 1-2 seconds in order to give my app zoom hovering the incoming call screen and also that my activity shouldn't have a view.
I'm kinda new to android, could anyone provide an example or a link that relates to my problem?

Related

How to navigate to a fragment from a class extending BroadcastReceiver using the new Navigation Architecture Components

I am having my Broadcast Receiver which is supposed to scan incoming messages and pass the originating address and message to a new fragment using bundles and using the new Navigation Architecture Components and navController. I am stuck because i cant find a view in the Broadcast Receiver. Here is what i have tried so far.
public class SimpleSmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages = SmsMessage.createFromPdu((byte[])pdus[0]);
Bundle bundle = new Bundle();
bundle.putString("MessageNumber", messages.getOriginatingAddress());
bundle.putString("Message", messages.getMessageBody());
Navigation.findNavController(context).navigate(R.id.nav_otp_fragment, bundle);
}
}
I am getting the error Required Type: View Provided: Context
BroadcastReceivers are not UI components. You can't do anything with the UI in a BroadcastReceiver. If your BroadcastReceiver has a reference to your Activity then it can call a method in the Activity (with data as arguments) so that the Activity can then create the Fragment and do whatever else is necessary.
Put your code inside runOnUiThread block
runOnUiThread {
// do something for UI
}

Call or text back to a private number

I'm using BroadcastReceiver to determine that calls or messages are from private numbers.
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Bundle onReceiveBundle = intent.getExtras();
String phoneNumber = onReceiveBundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
The phoneNumber returns negative numbers which cannot be called or replied back. Without the need to decode it, is it possible to reply or call back to a private number? If not, how can I decode it?

How to send data from BroadcastReceiver to Fragment in android

I am trying to make an chatting app.I have a SlidingDrawer Activity and it has many fragments and among which ChatFragment is one.Now when i am sending message to my friend , i have a Chatting Window and if any message comes from GCM service and i am in ChatFragment then this message will go to that Fragment and update the listview as i want to update the chatWindow when any message comes while chatting Now I tried to do like below.
GcmIntentService :
public class GcmIntentService extends IntentService {
public GcmIntentService() {
super("GcmIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (extras != null && !extras.isEmpty()) { // has effect of unparcelling Bundle
// Since we're not using two way messaging, this is all we really to check for
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Logger.getLogger("GCM_RECEIVED").log(Level.INFO, extras.toString());
showToast(extras.getString("message"));
Intent sendData = new Intent("chatupdater");
sendData.putExtra("msg", extras.getString("message"));
LocalBroadcastManager.getInstance(this).sendBroadcast(sendData);
Log.i("chat","i am in GCMIntentService");
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
Here i am starting a broadcastreceiver in onHandleIntent().
ChatBroadCastReceiver:
public class ChatBroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("chat","I am in ChatBroadCastReceiver");
String msg = intent.getStringExtra("msg");
Log.i("chat",msg);
//Intent data = new Intent("chatupdater");
//Intent data = new Intent("chatupdater");
//data.putExtra("key","data");
//data.putInt("fragmentno",1); // Pass the unique id of fragment we talked abt earlier
//context.sendBroadcast(intent);
}
}
This is my ChatBroadCastReceiver class and if any message comes it successfully receives at the onReceive() method.Now i want to send this message to the Fragment.What i tried , i registered the Fragment with it and tried to get the same data at the onReceive() of ChatFragment.But it didn't call.I tried to see by logging.
public class ChatFragment extends Fragment {
//ChatBroadCastReceiver mReceiver;
private EditText et_chat;
Bundle mailData;
String caller_mail;
private ListView chatListview;
private ChatWindowAdapter chatWindowAdapter;
private List<ChatSession>PreviousChatSession;
private List<ChatInfo> chatListItems;
Button chat_send;
public ChatFragment() {
}
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("chat","I am in BroadCastReceiver");
String msg = intent.getStringExtra("msg");
Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReceiver = new ChatBroadCastReceiver();
//getActivity().registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));
Log.i("chat", "I am in onCreate");
}
Now how can i get the message which i got in the Broadcastreceiver to the onReceive() of ChatFragment??
Try this
Create an interface which will have a method like this.
public interface DemoListener {
public void receiveMessage(String message);
}
Now implement this in your fragment and register the listener in BroadcastReceiver, now as soon as your broadcast receiver receives any message it will be available to your fragment via this Listener.Hope this helps.
To Register your Broadcast You are using:
mReceiver = new ChatBroadCastReceiver();
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"));
So you will Only receive Broadcast on ChatBroadCastReceiver class. Because mReceiver is a Instance of ChatBroadCastReceiver class.
Now to Receive broadcast Message on anonymous class that you have created in your ChatFragment you need to register that like below code. Where mReceiver is a Instance of anonymous BroadcastReceiver class that you have implemented in your ChatFragment class.
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"))
To Receive Broadcast Message Both on ChatFragment and ChatBroadCastReceiver you need to register broadcast receiver twice.
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"))
ChatBroadCastReceiver mChatBroadCastReceiver = new ChatBroadCastReceiver();
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mChatBroadCastReceiver ,new IntentFilter("chatupdater"))
Hope this answer will help you to understand your problem.

send data from a broadcast to an activity . how to do send?

how to send data from a broadcatReceiver to an activity in android as saying catch the received SMS and send the SMS (is the data) to the activity its my first time that i work with broadcatReceiver so can anyone help me i will appreciate any help.
i read several tutorials about the data transferring that have some steps :
create a class that extends activity
create a class that extends broadcastReceiver
but i did not know how is the communication between these 2 classes.
I would do something like that:
public class YourActivity extends Activity
{
private Handler handler = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.main_layout);
this.handler = new Handler() {
#Override
public void handleMessage(Message msg)
{
SmsMessage sms = (SmsMessage) msg.obj;
String senderNumber = sms.getOriginatingAddress();
}
};
// Register a new receiver that will trigger on SMS_RECEIVED event
IntentFiler filer = new IntentFilert("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSmsReceiver, filter);
}
#Override
protected void onDestroy()
{
super.onDestroy();
// Unregister the receiver
unregisterReceiver(mSmsReceiver);
}
private mSmsReceiver = new BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle == null || bundle.containsKey("pdus") return;
// Decode the message
SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[0]);
// Notify the activity with the message
Message msg = new Message;
msg.obj = sms;
YourActivity.this.handler.sendMessage(msg);
}
};
}
The receiver and the Activity are separate entities and do not usually interact directly. Imagine the following scenario: you have a receiver getting lots of SMS messages, and every time you get one, you launch an Activity to show the message received. Wouldn't this be very annoying to the user?
I'd says that you can interact with the user by creating a Notification and if the user clicks on it, then you open the Activity you want to show the details.
As how to pass the data to the Activity (hopefully using a notification first), given that SMS messages are short in nature you can just put the data in the Intent.
How to set a Notification to launch an Activity: see Open application after clicking on Notification

How to determine the phone number of a current caller in a stand-alone application

I'd like to build an Android application that can contact the current caller via a pre-determined text message. Sending a text message is simple enough but determining the phone number of the current caller in a stand-alone application is the challenge. Is the there an easy way to divine the phone number so I can send them a message while still on the call?
Of course there are manual ways to do this: write down the number, key it into a new text message, enter the message. But I want to define the message up front and be able to "send it to current caller".
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
helper = new ContactDatabaseHelper(context);
list = helper.getAllContacts();
try{
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (list.size() != 0){
for ( int i = 0, size = list.size(); i < size; i++ ){
if (PhoneNumberUtils.compare(incomingNumber, list.get(i).getContactNumber())){
ToastMsg.showToast(context,list.get(i).getContactName()+" Calling");
}
}
}
}catch (Exception e) {
// TODO: handle exception
}
}
public class PhoneCallStateListener extends PhoneStateListener{
private Context context;
public PhoneCallStateListener(Context context){
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
break;
case PhoneStateListener.LISTEN_CALL_STATE:
}
super.onCallStateChanged(state, incomingNumber);
}
}
For your sistuation the best I can think of is to use PhoneStateListener. It contains onCallStateChanged handler. One of the arguments is a String containing the incoming phone number.
Source:
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
Ctrl + F and type in "Incoming" and you will find everything you need to know.
EDIT: To make sure you're app starts on the startup of your phone, just add a BroadcastReciever. How to start an Application on startup?
Register a BroadcastReceiver in your manifest that listens to ACTION_PHONE_STATE_CHANGED.
Broadcast intent action indicating that the call state (cellular) on
the device has changed.
The EXTRA_STATE extra indicates the new call state. If the new state
is RINGING, a second extra EXTRA_INCOMING_NUMBER provides the incoming
phone number as a String.
Requires the READ_PHONE_STATE permission.
This was a sticky broadcast in version 1.0, but it is no longer
sticky. Instead, use getCallState() to synchronously query the current
call state.
This way you don't need the user to launch your app before receiving a call.

Categories

Resources