1

내 전화 걸기 (기본 또는 주식 걸기)를 열 때 서비스를 시작하고 싶습니다. 나의 서비스는 단지 메시지를 건배한다. 전화 걸기 서비스를 시작하는 방법. startService 명령은 MainActivity에서 작동하지만 다이얼러가 열려있는 동안에는 작동하지 않습니다.android의 전화 걸기 (기본 또는 주식 걸기)에서 서비스를 시작하는 방법

내 코드는 다음과 같습니다 :

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>  

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.chatheads"> 

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
<uses-permission android:name="android.permission.DIAL_ACTION" /> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 


    <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service 
     android:name=".MyService" 
     android:enabled="true" 
     android:exported="true" /> 

    <activity android:name=".SecondActivity"></activity> 
</application> 

</manifest> 

MainActivity.java

package com.example.chatheads; 

import android.support.v7.app.AppCompatActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.method.DialerKeyListener; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 


public class MainActivity extends AppCompatActivity { 
Button startService,stopService; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    startService=(Button)findViewById(R.id.startService); 
    stopService=(Button)findViewById(R.id.stopService); 




    if(getIntent().getAction().equals(Intent.ACTION_DIAL)) { 
     Intent intent = new Intent(getBaseContext(), MyService.class); 
     startService(intent); 

    } 

    startService.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      startService(new Intent(getApplication(), MyService.class)); 




     } 
    }); 
    stopService.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      stopService(new Intent(getApplication(), MyService.class)); 



     } 
    }); 


    } 
} 

Myservice.java

package com..service; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class MyService extends Service { 
    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
    super.onCreate(); 
    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show(); 

    } 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
    intent.getStringExtra("MY_MESSAGE"); 
    //stopSelf(); 
    return super.onStartCommand(intent, flags, startId); 
} 
@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show(); 

} 

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

} 

이 코드는 작동하지 않습니다. MainActivity에서 startService를 호출 할 때 시작되는 서비스입니다. 그러나 전화 걸기가 열리지 않을 때 작동하지 않습니다.

답변

0

getApplication()MainActivity.this으로 변경할 수 있습니다.

그리고 서비스를 시작하기 위해 다이얼러에서 MainActivity으로 이동해야한다고 생각합니다.

관련 문제