【问题标题】:how to make notification when end incoming call android如何在结束来电时发出通知android
【发布时间】:2014-02-21 04:58:32
【问题描述】:

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

【问题讨论】:

    标签: java android call android-notifications


    【解决方案1】:

    替换

    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 实现类中查找实际上不是的方法。

    【讨论】:

    • 现在很好,但是我将什么上下文传递给 PendingIntent 和通知?我想在通话被拒绝时显示通知
    • @user3106818 尝试使用Context 返回的context.getApplicationContext()...PendingIntent pi= PendingIntent.getActivity(context.getApplicationContext(), 0, intent, 0);
    猜你喜欢
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多