1

Toast当通话状态为空闲时,我想调出一个并振动。我在清单文件中添加了这个:

<receiver android:name="IncomingCallInterceptor"> 
      <intent-filter> 
         <action android:name="android.intent.action.PHONE_STATE"/> 
      </intent-filter> 
    </receiver>

这是 IncomingCallInterceptor

import android.content.BroadcastReceiver; 
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
 import android.widget.Toast;
 import android.app.Activity;



public class IncomingCallInterceptor extends BroadcastReceiver { 
     @Override
    public void onReceive(Context context, Intent intent)
    {                                        
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                        
        String msg = "Phone state changed to " + state;

        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
       {                                  
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            msg += ". Incoming number is " + incomingNumber;


        }

        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
        if(msg == "IDLE")
        {
          ok() ;
    }

    }
    public void ok() 
    { 


        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(3000);
    }
     }
}

我收到了这个错误:

Description Resource    Path    Location    Type
The method getSystemService(String) is undefined for the type IncomingCallInterceptor   IncomingCallInterceptor.java        line 39 Java Problem
4

1 回答 1

0

确保您已在清单中添加了 VIBRATE 权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
    <uses-permission android:name="android.permission.VIBRATE"/>
    <application android:label="...">
        ...
    </application>
</manifest>

将您的代码更改为:

public class IncomingCallInterceptor extends BroadcastReceiver { 
private Context contextc;
 @Override
public void onReceive(Context context, Intent intent)
{                             
this.contextc=context;
//YOUR CODE HERE

public void ok() 
{ 
    Vibrator v = (Vibrator)contextc.getSystemService(Context.VIBRATOR_SERVICE);
    v.vibrate(3000);
}
于 2012-06-24T11:38:22.520 回答