public IBinder onBind(Intent intent)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
try
{
if(intent.getStringExtra("Number")!=null)
{
MobileNo=intent.getStringExtra("Number");
Log.i("MObileNo", MobileNo);
MainActivity.CheckPaymentGivenOrNot(MobileNo,getApplicationContext());
}
}
catch(Exception e)
{
//MainActivity.CheckPaymentGivenOrNot("8140499976",getApplicationContext());
}
return Service.START_STICKY;
}
@SuppressLint("NewApi")
@Override
public void onTaskRemoved(Intent rootIntent)
{
Log.d("startttttttttttttttt", "start Service");
ComponentName receiver = new ComponentName(getApplicationContext(), CallReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
IntentFilter intentfilter=new IntentFilter();
intentfilter.addAction("android.intent.action.PHONE_STATE");
registerReceiver(cr, intentfilter);
sendBroadcast(new Intent("StartKilledService"));
}
@SuppressLint("HandlerLeak")
@Override
public void onCreate()
{
Log.d("hello", "hello");
h = new Handler()
{
@Override
public void handleMessage(Message msg)
{
//sendBroadcast(new Intent("StartKilledService"));
}
};
t = new Thread()
{
@Override
public void run()
{
while(b)
{
try
{
Thread.sleep(10);
h.sendEmptyMessage(0);
}
catch (InterruptedException e)
{
}
}
}
};
t.start();
super.onCreate();
}
@Override
public void onDestroy()
{
ComponentName receiver = new ComponentName(getApplicationContext(), CallReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
//sendBroadcast(new Intent("StartKilledService"));
super.onDestroy();
}
}
我已经定义了这个服务类我很累,因为当我申请关闭服务没有启动并且服务自动被杀死而没有检测到来电时请帮助我。
答案 0 :(得分:1)
无需创建任何服务。只需像这样创建一个BroadcastReceiver:
public class CallReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}
public class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state,String incomingNumber){
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "CALL_STATE_IDLE : " + incomingNumber);
// CALL_STATE_IDLE;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
Log.d("DEBUG", "CALL_STATE_OFFHOOK : " + incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING : " + incomingNumber);
break;
default:
break;
}
}
}
}
在Menifest文件中添加读取手机状态的用户权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
将它放在Menifest文件中的标签内:
<receiver android:name=".CallReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
如果您仍想使用服务并面临服务未在手机BOOT上启动的问题,请按以下步骤操作:
首先,您需要AndroidManifest.xml中的权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
同时将这些添加到清单文件中:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
然后使用BroadcastReceiver在手机BOOT事件上重启您的服务:
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
}
}