java - 如何在结束来电android时发出通知

标签 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();
    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) {
     //
}

当您在 Runnable 中调用 getSystemService() 时,它会在 Runnable 实现类中查找该方法,而实际上不是。

关于java - 如何在结束来电android时发出通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21925630/

相关文章:

Python:这两个函数有什么问题?

java - 面向对象编程与抽象数据类型编程的区别(多态性)

java - 如何在Java中正确组织和嵌套包

java - GXT : Cancel click event on grid header

java - hibernate 中的可变长度Blob?

android - 通过 Google Drive API 从 MS Word 文档更新现有的原生 Google Drive 文档

android - 在 Android Studio 中调试服务的正确方法?

Android HttpURLConnection 抛出 EOFException

python - 从另一个 python 脚本调用带有参数的 python 脚本

javascript - 'call' 在 javascript 中如何工作?