Reputation: 198
I want to reject call after 1 sec and make notification But when i make notification then it give syntax error in getSystemService What can i do
Here is my code for rejecting call and make notification
private String incomingnumber;
@Override
public void onReceive(Context context, Intent intent) {
String s[] = { "045193117", "800000000" };
Bundle b = intent.getExtras();
incomingnumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
try {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
final com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m
.invoke(tm);
for (int i = 0; i < s.length; i++) {
if (s[i].equals(incomingnumber)) {
Runnable mMyRunnable2 = new Runnable() {
@Override
public void run() {
try {
telephonyService.endCall();
NotificationManager nm= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify= new Notification(android.R.drawable.ic_lock_idle_low_battery, "Battery Notification",SystemClock.currentThreadTimeMillis());
CharSequence title="Battery Level ";
CharSequence details= "Continue with what you were doing";
Intent intent= new Intent(context, MainActivity.class);
PendingIntent pi= PendingIntent.getSctivity(con, 0, intent, 0);
notify.setLatestEventInfo(con, title, details, pi);
nm.notify(0, notify);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Handler myHandler = new Handler();
myHandler.postDelayed(mMyRunnable2, 1000);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1
Views: 1776
Reputation: 11131
replace
NotificationManager nm= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
with
NotificationManager nm= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
and make the context
parameter as final
to access it inner class (here Runnable
).
public void onReceive(final Context context,Intent intent) {
//
}
when you say getSystemService()
in Runnable
, it will look for that method in Runnable
implementation class which is actually not.
Upvotes: 2