我想在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();
    }
}

最佳答案

更换

NotificationManager nm= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);




NotificationManager nm= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


并将context参数设置为final,以访问其内部类(此处为Runnable)。

public void onReceive(final Context context,Intent intent) {
     //
}


当您在getSystemService()中说Runnable时,它将在Runnable实现类中查找实际上不是的方法。

10-08 09:22