PhoneNumberUtils.formatNumberToE164 indesired method result - android

I am trying to format a phone number to E164 , in vain :
//Detects outgoing call :
private class MyOutCallsReceiver extends BroadcastReceiver{
public MyOutCallsReceiver() {super();}
#Override
public void onReceive(Context context, Intent intent) {
String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.v("OUTCALL-outgoing",outgoingNumber);
String phone = MyApplication.TryFormatPhoneNumberToE164(outgoingNumber);
Log.v("OUTCALL-phone",phone);
}
Logcat:
11-11 14:23:19.836 495-495/ma.altaiir.app V/OUTCALL-outgoing:
0612345678
11-11 14:23:19.841 495-495/ma.altaiir.app V/OUTCALL-phone:
0612345678
Formatting method :
public static String TryFormatPhoneNumberToE164(String phone){
String result = PhoneNumberUtils.formatNumberToE164(phone,_CountryIsoCode);
if(result == null){result = PhoneNumberUtils.formatNumberToE164(PhoneNumberUtils.normalizeNumber(phone),_CountryIsoCode);}
Log.v("COUNTRYISOCODE/NUMB",_CountryIsoCode + "/" + phone + "/" + result);
if(result == null){return phone;}else{ return result;}
}
Logcat:
11-11 14:23:19.841 495-495/ma.altaiir.app V/COUNTRYISOCODE/NUMB:
ma/0612345678/null
All this means , the method is just returning null whatever I do to avoid this, is this known drawback or it is something I do wrong ?

Finally i ended up using libphonenumber wich resolved all my number parsing problems, but still wondering why builin method do not work !
Anyways, for any one facing the sale parsing problems, try https://github.com/googlei18n/libphonenumber

Related

using Object... in codenameone

I'm using Codenameone to develop application in mobile. I want to create a method to show an error message on Screen. But I got an error:
This is my code
public class Common {
public static boolean checkNullOrEmpty(String value){
return !(value != null && !value.equals(""));
}
public static void showMessage(String title,String msgID, Object... params){
String result = String.format(msgID, params);
Dialog.show(title, result, "OK", "Cancel");
}
}
And this is the way I call that method:
Common.showMessage("Error", "Item %s ; Item %s","01","02");
This is error message:
error: cannot find symbol
String result = String.format(msgID, params);
symbol: method format(String,Object[])
location: class String
Can anybody help me? Thanks a lot.
String.format isn't supported by the Codename One subset of the Java API. You should be able to use something like StringUtil.replaceAll etc. to replace entries e.g. for this:
Common.showMessage("Error", "Item {0} ; Item {1}","01","02");
You should be able to do something like this:
String result = msgID;
for(int iter = 0 ; iter < params.length ; iter++) {
result = StringUtil.replaceAll(result, "{" + iter + "}", params[iter]);
}

How to detect whether it's the same StatusBarNotification

In my app, I have a NotificationListenerService that listens to all notifications. I have a StatusBarNotification field that is assigned when some notification is posted and nullified when it is removed. Before nullifying, I have to check whether or not it is the same StatusBarNotification that I have assigned before. However, the check with == operator does not work as expected even though it's exactly the same notification. So how can I compare them?
public class NotificationListener extends NotificationListenerService {
private StatusBarNotification targetNotification;
#Override
public void onNotificationPosted(StatusBarNotification notification) {
if (targetNotification == null && notification.isClearable()) {
targetNotification = notification;
}
}
#Override
public void onNotificationRemoved(StatusBarNotification notification) {
System.out.println("removed noti: " + notification.getPackageName() + ", " + notification.getPostTime()+", "+notification.getId()+", "+notification.getUserId());
System.out.println("target noti: " + targetNotification.getPackageName() + ", " + targetNotification.getPostTime()+", "+targetNotification.getId()+", "+targetNotification.getUserId());
System.out.println(notification == targetNotification);
if (notification == targetNotification) {
targetNotification = null;
}
}
}
Here is the result:
removed noti: com.samepackage, 1412915524994, -99, 0
target noti: com.samepackage, 1412915524994, -99, 0
false
== compare objecs which point to same memory location.(
http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/)
so instead of comparing to objects please compare it's id value. this might be solve your problem.
if (notification.getId() == targetNotification.getId()) {
targetNotification = null;
}
From the documentation of onNotificationRemoved (StatusBarNotification sbn):
Parameters
sbn: A data structure encapsulating at least the original
information (tag and id) and source (package name) used to post the
Notification that was just removed.
So I think to compare two notifications, we need to compare their tag, id, and package name:
if (notification.getTag().equals(targetNotification.getTag()) &&
notification.getId() == targetNotification.getId() &&
notification.getPackageName().equals(targetNotification.getPackageName())) {
targetNotification = null;
}
Edit: Be careful, notification tags can be null! (So, the code above might throw NPE). Make sure to control it.

Sent SMS observer has double the output

I'm trying to make an SMS observer for outgoing messages using the ContentObserver. The code below works perfectly, but whenever I test this by sending a text message to a colleague, I get the output twice.
The Observer gets registered in a Service, like this:
SentSMSObserver sentSMSObserver = new SentSMSObserver(new Handler(), this);
getContentResolver().registerContentObserver(sentSMSObserver.CONTENT_SMS_URI, true, sentSMSObserver);
Whenever I send a textmessage to my own number, I only get the output once, which is really weird. As a Service is a Singleton (as far as my research has led me to believe), it's quite improbable there is a second instance of my observer.
public class SentSMSObserver extends ContentObserver {
private static final String CONTENT_SMS = "content://sms";
public final Uri CONTENT_SMS_URI = Uri.parse(CONTENT_SMS);
private Context context;
public SentSMSObserver(Handler handler, Context context) {
super(handler);
this.context = context;
}
#Override
public void onChange(boolean selfChange) {
Cursor cursor = context.getContentResolver().query(CONTENT_SMS_URI, null, null, null, null);
try {
if (cursor.moveToNext()) {
String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
int type = cursor.getInt(cursor.getColumnIndex("type"));
if (protocol != null || type != Telephony.TextBasedSmsColumns.MESSAGE_TYPE_SENT) {
return;
}
String to = cursor.getString(cursor.getColumnIndex("address"));
Date now = new Date(cursor.getLong(cursor.getColumnIndex("date")));
String message = cursor.getString(cursor.getColumnIndex("body"));
Log.e("sentmessage", to + " - " + now + " - " + message);
}
} finally {
cursor.close();
}
}
}
Logcat:
08-11 14:25:14.292 12574-12574/com.androidfun.smstest E/sentmessage﹕ +(deleted phone n°) - Mon Aug 11 14:25:10 CEST 2014 - Test
08-11 14:25:18.306 12574-12574/com.androidfun.smstest E/sentmessage﹕ +(deleted phone n°) - Mon Aug 11 14:25:10 CEST 2014 - Test
Sent messages are also updated with "sent confirmation" and even "delivery confirmation" in some cases. Sending it to yourself, you probably are not getting a "sent confirmation" because it sends and confirms in one step.
See the post here:
SMS sent observer executes 3 times

Android - remove missed call notification

is there anyway to remove a missed call notification by code? And somehow remove the last missed call from call history?
yes, it is possible.Try this:
Uri UriCalls = Uri.parse("content://call_log/calls");
Cursor cursor = getApplicationContext().getContentResolver().query(UriCalls, null, null, null, null);
Reading call log entries...
if(cursor.getCount() > 0){
cursor.moveToFirst();
while(!cursor.isAfterLast()){
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); // for number
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going
cursor.moveToNext();
}
}
Deleting entry in call log...
String queryString= "NUMBER='" + number + "'";
if (cursor.getCount() > 0){
getApplicationContext().getContentResolver().delete(UriCalls, queryString, null);
}
Permission:
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
Note: Please refer this doc over call log for more clearity.
Using the above code you can get the desired result.
Refer to this
You can clear your missed call by calling cancel(ID) or calcelAll() to clear your notifications bar.
For the part where you want to remove the last call from the log you need to move the method that is deleting the entry into a class which is a subclass of the Thread class. This allows you to then set it to sleep for a short period to allow Android to actually write to the call log BEFORE you you run the delete query. I had the same problem, but manage to resolve it with the code below:
public class DelayClearCallLog extends Thread {
public Context context;
public String phoneNumber;
public DelayClearCallLog(Context ctx, String pNumber){
context = ctx;
phoneNumber = pNumber;
}
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
clearCallLog(context, phoneNumber);
}
public void clearCallLog(Context context, String phoneNumber) {
// implement delete query here
}
}
Then call the the method as follows:
DelayClearCallLog DelayClear = new DelayClearCallLog(context, phoneNumber);
DelayClear.start();

android function in class throws java.lang.NullPointerException

I've made a class which holds some string and integers, in that class I made a function to convert the data in the class in to a readable string;
public String GetConditions() {
String BigString = null;
String eol = System.getProperty("line.separator");
try {
BigString += "Depth: " + ci(Depth) + eol;
and so on...
Because I have to convert many integers, I made an extra function to convert a integer to a string;
public String ci(Integer i) {
// convert integer to string
if (i != null) {
String a = new Integer(i).toString();
return a;
} else {
return "n/a";
}
}
This throws a NullPointerException exception on return a. I'm quite new to Java, this is probally a noob question... Sorry about, thanks in advance!
There is a much simpler way to convert an Integer to a String: use String#valueOf(int).
public String ci(Integer i)
{
return i == null ? "n/a" : String.valueOf(i);
}
Try converting the Integer you pass in your method to string, instead of instantiating a new one.
You can do it straight forward like:
String a = i.toString();
or
String a = Integer.toString(i.intValue());
Thanks guys, but I found the problem, I've tried to add something to a string which was 'null' , this line:
String BigString = null;

Categories

Resources