我如何在BroadcastReceiver类中使用Service类

时间:2014-03-08 06:58:56

标签: android broadcastreceiver android-service

我想通过BroadcastReceiver类启动Service类 我正在运行这个应用程序“电话正在运行”,但我没有得到第二个吐司,即“在服务中” 我所做的编码是......

public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub


            Intent i=new Intent(context,Myserve.class);
            context.startService(i);
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Toast.makeText(context, "Phone is Ringing", Toast.LENGTH_LONG)
                        .show();
}
}

class Myserve extends Service{


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        Toast.makeText(getApplicationContext(), "in service", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

0 个答案:

没有答案