Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在结束来电时发出通知_Java_Android_Call_Android Notifications - Fatal编程技术网

Java 如何在结束来电时发出通知

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

我想在1秒后拒绝电话并发出通知 但当我发出通知时,它会在getSystemService中给出语法错误 我能做什么

这是我拒绝呼叫并发出通知的代码

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) {
     //
}