2014-04-10 2 views
0

그래서 바로 가기에서 서비스를 시작하고 싶습니다. 직접 할 수는 없다는 것을 알고 있으므로 서비스를 시작하는 유일한 목적으로 활동을 설정했습니다. 내 서비스의 목적은 다른 앱에 의도를 보내고 5 초 후 다른 앱을 보내어 CountDownTimer를 사용하여이 작업을 수행하는 것입니다. 그러나 바로 가기 (이 혼란스러워지고있다)에서 서비스를 시작하는 활동을 시작하면 앱 UI가 실행됩니다. 나는 이것을 배경 서비스가되기를 원하기 때문에 이것을 원하지 않는다. 내가 뭘 잘못하고있어. 나는 방금 개발에 착수했습니다. 그래서 그것은 명백한 것이 될 수 있습니다. 그러나 저는 며칠 동안이 문제에 대해 지금까지 싸워 왔습니다.바로 가기를 통한 백그라운드 서비스 실행

몇 가지 이유로 서비스에서 실행하면 앱이 바로 시작됩니다. 보이지 않는 활동에서 바로 실행하면 정상적으로 5 초 동안 제대로 실행 된 다음 앱이로드됩니다. 왜 앱을로드하는지 알 수 없습니다.

나는 관련성이있는만큼 많은 정보를 포함 시켰습니다. 도움을 주시면 감사하겠습니다.

내 서비스 :

public class Pop1_5Service extends IntentService { 

    public Pop1_5Service() { 
     super("Pop1_5Service"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     // Normally we would do some work here, like download a file. 
     // For our sample, we just sleep for 5 seconds. 
     new CountDownTimer(5000, 2500) { 
      public void onTick(long millisUntilFinished) { 
       Intent i = new Intent(INTENT_ACTION); 
       Bundle b = new Bundle(); 
       b.putInt(BUNDLE_VERSION_CODE, 1); 
       b.putString(BUNDLE_STRING_NAME, "POP1"); 
       b.putString(BUNDLE_STRING_VALUE, "1"); 
       i.putExtra(BUNDLE_NAME, b); 
       sendBroadcast(i);   } 
      public void onFinish() { 
       Intent i = new Intent(INTENT_ACTION); 
       Bundle b = new Bundle(); 
       b.putInt(BUNDLE_VERSION_CODE, 1); 
       b.putString(BUNDLE_STRING_NAME, "POP1"); 
       b.putString(BUNDLE_STRING_VALUE, "1"); 
       i.putExtra(BUNDLE_NAME, b); 
       sendBroadcast(i);   } 
      } 
     }.start(); 
    } 
} 

활동 시작 서비스 :

매니페스트의
public class Pop1_5Activity extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState);  

    Intent intent = new Intent(this, Pop1_5Service.class); 
     startService(intent); 
     finish(); 
    } 
} 

하위 섹션 :

<activity 
    android:name=".Pop1_5Activity" 
    android:theme="@android:style/Theme.NoDisplay"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
<service android:name=".Pop1_5Service" /> 

그리고 활동 '바로 가기 만들기':

public class CreateShortcutActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Intent shortcutintent = new Intent(this, Pop1_5Activity.class); 
     ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); 

     Intent intent = new Intent(); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutintent); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Pop1_5"); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 
     setResult(RESULT_OK, intent); 
     finish(); 
    } 
} 

답변

0

사물의 모양에서 보면 CreateShortcutActivity는 아무 것도하지 않는 것처럼 보입니다. LAUNCHER는 Pop1_5Activity이므로 사용자가 앱 아이콘을 누르면이 Activity가 실행되고 서비스가 시작됩니다.

당신이 우리에게 보여준 모든 코드는 "보이지 않습니다", 두 가지 활동이 끝나고 서비스는 서비스입니다.

BroadcastReceiver가 브로드 캐스트를 처리하는 방법을 살펴볼 수 있습니다. 예를 들어, PendingIntent를 통해 다른 Activity를 생성합니까? 활동이 보이지 않게 만들어 졌습니까?

아마도 BroadcastReceiver에서 활동이 보류중인 대신 보류중인 서비스를 만들어야합니다.

+0

응답 해 주셔서 감사합니다. 전체 매니페스트가 아닙니다. 전체 응용 프로그램을 시작하는 'MainActivity'에 대한 또 다른 실행 프로그램이 있습니다. 나는 [이] (http://kind-kristiansen.no/2010/android-adding-desktop-shortcut-support-to-your-app/)의 인상을 받았다. 사용자는 바로 가기를 만들고 싶습니다. 바로 가기를 시작하려는 활동에 인 텐트 필터를 추가했습니다.이 필터를 사용하지 않으면 시작할 수 없으므로 ... – user3462640

+0

다르게 설명 할 수 있습니까? ** "바로 가기에서 서비스를 시작하겠습니다. ** ** 보이지 않는 활동 Pop1_5Activity (이 App1을 호출합시다.)는 이미 그 자체로 바로 가기이며, 원하는 서비스를 시작합니다. 너 세트있어. ** "내 서비스의 목적은 다른 앱에 의도를 보내는 것입니다. ** 나는 앱이 출시되었다고 말한 이래로 이미 제대로하고 있다고 생각합니다. 정보 만 전송하는 경우 BroadcastReceiver에서 처리하는 것이 좋습니다 (SharedPreferences에 저장하는 것이 더 좋을까요?). PendingIntent.getActivity()를 수행하지 마십시오. – lemuel

+0

그래, 내가 처음부터 이걸 가지고 시작해야한다고 생각하는데, 나는 완전히 잘못 될 것 같아. 시간을내어 주셔서 감사합니다. – user3462640

관련 문제