开发者

how to get context from PhoneStateListener class

i need to call method from several classes, but i don't know how to get the right context

the holding class:

public class SharedData {
......
......

    public static void stop_ring(Context context){

        Uri  lcurUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION);
        Ringtone ring = RingtoneManager.getRingtone(context, lcurUri);

        ring.stop();
    }

how can i call it from activity class, and how can i call it from P开发者_如何学编程honeStateListener class.


Activity extends Context so you can call it like so:

SharedData.stop_ring(this);

For a listener you will have to put Context in the constructor and save it as a property. Then call:

SharedData.stop_ring(saved_context);


the major solution is

1.

public class MyPhoneStateListener extends PhoneStateListener 
{
    public MyPhoneStateListener(Context ctx) {
    super();        
}

2.

When istance the listener

TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);telephonyManager.listen(
                       new MyPhoneStateListener(Context),
                       PhoneStateListener.LISTEN_CALL_STATE);

3.

finish

by


Try to use getApplicationContext(). Mostly it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜