C2DM - onMessage, interactivity with results I get - android

I am successfully getting messages from C2DM service. Unfortunately I am not able to anyhow interact with them (set the string I get as a notification, or set it as a string in toast message,...). Is there any example how to do that?
SC:
#Override
protected void onMessage(Context context, Intent intent) {
Log.e("C2DM", "Message: arived");
Bundle extras = intent.getExtras();
if (extras != null) {
Log.d("DATA_ZE_SERVERU",extras.get("payload")+"");
//how to interact with data?
}
}
Thx
Nobody knows???

You can pass the string to a activity and let the activity show it. I did most of my c2dm like that.

Related

How to implement callback listener in BroadcastReceiver onReceive on notification tap?

I am creating an app that sends a notification when an SMS is received. When the notification is tapped, it opens the MainActivity and shows the received SMS. The problem is, when the app is closed and an SMS is received, the mListener inside the onReceive method of BroadcastReceiver class produces a null pointer exception (NPE). Logs show the error: "Attempt to invoke interface method 'void com.example.smsread.listener.SmsListener.messageReceived(java.lang.String)' on a null object reference".
What can I do so that the control can be passed to the activity from onReceive?
I can perform the action sending notification within the onReceive but doesn't seem appropriate. I wish to pass the message to the interface and let the activity implement it.
SmsReceiver.java
public class SmsReceiver extends BroadcastReceiver {
//interface
private static SmsListener mListener;
public static void bindListener(SmsListener listener) {
mListener = listener;
}
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = new Object[0];
if (bundle != null) {
pdus = (Object[]) bundle.get("pdus");
}
StringBuilder messageBody = new StringBuilder();
if (pdus != null) {
for (Object pdus1 : pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus1);
messageBody.append(smsMessage.getMessageBody());
}
}
//Pass the message text to interface
mListener.messageReceived(messageBody.toString()); // mListener here is null
}
}
I expect the mListener to pass the control to the MainActivity where the interface is implemented but mListener is found to be null. Please help as to what changes have to be done so that mListener points to the MainActivity.
If there is a better way to implement the scenario I wish to implement, suggestions are more than welcome.
Thanks in advance :)
You are trying to call function of an Activity that was already destroyed.
If you want to communicate with your activity from a BroadcastReceiver, Use Intent.
Create a new Intent with your Activity as target and send the message as as Extra in Intent Extras Bundle.
In your Activity, you can set launchMode to singleInstance or singleTask in your manifest file. This means that the Intent if sent and your Activity is already open, it will call onIntent function in your activity where you will call the listener function.
android:launchMode="singleInstance"
#Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
processDataFromBroadcast(intent);
}
Be sure to read about launch modes and singleInstance to fully understand what it means or you might get unexpected behavior:
https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManifestForTasks

Receive message SmartEyeglass ControlExtension

I want to make an app for Sony SmartEyeglass where the App on the Phone and the ControlExtension exchange data on runtime.
It's pretty obvious how to send messages from the app to the extension...
public void startExtension(String msg) {
if (HelloWorldExtensionService.Object != null) {
HelloWorldExtensionService.Object
.sendMessageToExtension(msg);
}
}
but how do I get the msg in my ControlExtension, if the extension is already running?
I didn't find an onMessageReceived(String message) method for the class ControlExtension.
You can do this using Intents the same way you would for an standard Android app. For example from your Activity:
Intent intentBuzz = new Intent();
intentBuzz.setAction(buzzIntent);
mContext.sendBroadcast(intentBuzz);
Then in your Control Extension register a broadcast receiver:
buzzReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
buzzAction();
}
};
registerReceiver(buzzReceiver, new IntentFilter(buzzIntent));
This works the other way around as well if you want to pass something other than a string.

BroadcastReceiver for call answers

I'm trying to programmatically intercept a call answer.
I've got a BroadcastReceiver with the following method:
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if(state.equals("OFFHOOK")) {
// ...
}
// ANSWER ??
}
}
While the code works successfully if the phone receives a call (offhook is related to the answer, because before I've got the ringing state) I can't detect the answer relatively to a placed call: in this case offhook is relative to the ringing.
How can I intercept the answer relatively to a placed call?

How to filter incomming calls (blacklist) - no reflection

I was wondering if there is a way that I can filter (block) incoming calls on Android (consider 2.1 and up). I found solutions using reflection, but it seem not to be very clean and reliable solution. Is there any standard or google recommended way to do that?
UPDATE: Anyone?
use the following broadcast receiver to get the incoming phone number and compare it with the numbers that are in your created filter list
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, phoneNumber, 2000).show();
Log.w("DEBUG", phoneNumber);
}
}
}
Hope it will help. You need to create a list of numbers in blacklist by your application's User interface.

How can I hide/cancel the default incoming screen

I am currently intercepting a call and forwarding it to my customized "oncallscreen" activity. However, before getting to my screen it will flash to the default "oncallscreen". How can I hide/cancel this screen so when receiving call I get only my customized screen.
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
You need to intercept broadcast android.intent.action.PHONE_STATE with highest possible priority, then if in your BroadcastReceiver.onReceive() you will cancel broadcast through BroadcastReceiver.abortBroadcast() you will be able to stop default incoming call screen to be shown, since default application won't receive incoming call broadcast. After that you're free to show your own Activity.

Categories

Resources