1

ViewPager가 포함 된 응용 프로그램을 코딩 중이며,이를 위해 조각을 사용하고 있습니다. 모든 것은 잘 작동합니다 (원하는대로).조각이 시작되지 않습니다.

ViewPager의 내 조각 중 하나에 서비스를 시작하는 버튼이 있습니다.

getActivity().startService(new Intent(getActivity(),Service.class))은 버튼의 코드입니다.

이제 버튼을 클릭해도 아무 일도 일어나지 않습니다. 거기에 또 다른 해결책이 있습니까?

자세한 정보와 코드가 필요한 경우 알려주십시오.

도움 주셔서 감사합니다.

+3

당신의 서비스를'manifest.xml'에 등록하셨습니까? –

+1

서비스를'AndroidManifest.xml'에 등록 하시겠습니까? – PageNotFound

+0

또한 적절한 수신기를 'Button'에 설정 했습니까? – curtisLoew

답변

1

이것은 거의 항상 매니페스트에 서비스를 등록하지 않는 것과 관련이 있습니다. 항상 나에게 일어난다. 매니 페스트에 <service android:name="your.package.name.YourService"/> 을 입력하기 만하면됩니다. MANIFEST DECLARE 서비스의

7

1) :

<service android:name=".GameService" /> 

2) 다음 WRITE 클래스 마녀 서비스를 확장 : (내 예제는 의도 Serivce)

public class GameService extends IntentService 
{ 
public GameService() 
    { 
     super("GameService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) 
    { 
     ..do some background work... 
    } 
} 

3) 방송 수신기 WITH MY FRAGMENT입니다 및 호출 서비스와 CODE :

public class Stats_Fragment extends Fragment 
{ 
BroadcastReceiver receiver = new BroadcastReceiver() 
    { 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      Bundle extras = intent.getExtras(); 

      if(extras!=null) 
      { 
       //for example: extras.getString("value"); 
      } 
     } 
    } 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View rootView = inflater.inflate(R.layout.fragment_stats, container, false); 

     //CALLING SERIVCE (I call it here but you can call somewhere else... 

     Intent intent = new Intent(getActivity(), GameService.class); 
     intent.putExtra("Intent_Action", 3); 
     intent.putExtra("playerID", MainClass.loggedUserID); 
     getActivity().startService(intent); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     getActivity().registerReceiver(receiver, new IntentFilter(GameService.NOTIFICATION)); 
     CallStatsService(); 
    } 

    @Override 
    public void onPause() 
    { 
     super.onPause(); 
     getActivity().unregisterReceiver(receiver); 
    } 

} 

내가 그것을 :)

012 도움이되기를 바랍니다
관련 문제