Java 如何在结束来电时发出通知
我想在1秒后拒绝电话并发出通知 但当我发出通知时,它会在getSystemService中给出语法错误 我能做什么 这是我拒绝呼叫并发出通知的代码Java 如何在结束来电时发出通知,java,android,call,android-notifications,Java,Android,Call,Android Notifications,我想在1秒后拒绝电话并发出通知 但当我发出通知时,它会在getSystemService中给出语法错误 我能做什么 这是我拒绝呼叫并发出通知的代码 private String incomingnumber; @Override public void onReceive(Context context, Intent intent) { String s[] = { "045193117", "800000000" }; Bundle b = intent.getExtras
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();
}
}
私有字符串输入编号;
@凌驾
公共void onReceive(上下文、意图){
字符串s[]={“045193117”,“80000000”};
Bundle b=intent.getExtras();
incomingnumber=b.getString(TelephonyManager.EXTRA\u传入\u号码);
试一试{
TelephonyManager tm=(TelephonyManager)上下文
.getSystemService(上下文.电话服务);
Class c=Class.forName(tm.getClass().getName());
方法m=c.getDeclaredMethod(“getITelephony”);
m、 setAccessible(true);
最终com.android.internal.telephony.ITelephony电话服务=(ITelephony)m
.invoke(tm);
对于(int i=0;i
更换
NotificationManager nm= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
与
并将context
参数设置为final
,以访问其内部类(此处Runnable
)
当您在
Runnable
中说getSystemService()
时,它将在Runnable
实现类中查找实际不是的方法。现在可以了,但我将什么上下文传递给pendingent和notification?我想在通话结束时显示通知rejected@user3106818尝试使用由Context.getApplicationContext()
返回的上下文pendingent pi=pendingent.getActivity(context.getApplicationContext(),0,intent,0)代码>
NotificationManager nm= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
public void onReceive(final Context context,Intent intent) {
//
}