2014-06-17 2 views
0

후에 내가 전화를 시작자동 시작 응용 프로그램을 즉시 부팅

난 그냥 here에서 자습서를 수행 할 때 내 응용 프로그램을 시작하고 싶지만 내 장치에서 작동하지 않습니다.

package net.londatiga.android; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class MyBroadcastReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent startServiceIntent = new Intent(context, ExampleActivity.class); 
     context.startService(startServiceIntent); 
    } 
} 

을 그리고 이것은 내 매니페스트

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="net.londatiga.android" 
     android:versionCode="2" android:versionName="1.01"> 
    <uses-sdk android:minSdkVersion="7" 
     android:targetSdkVersion="15"/> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

     <receiver android:name="net.londatiga.android.MyBroadcastReceiver" 
    android:enabled="true" 
    android:exported="true"> 
    <intent-filter android:priority="1000"> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

     <activity android:name=".ExampleActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 


    </application> 

</manifest> 

내 실수하십시오이다 : 내 방법을 참조하십시오?

context.startService(startServiceIntent); 

사용 : 대신의

+0

재부팅하기 전에 ExampleActivity를 시작하셨습니까? 시스템이 BOOT_COMPLETED 의도를 전달하기 위해 적어도 하나의 활동이 사용자에 의해 시작되어야합니다. – kupsef

+0

예 시작했습니다 – slama007

답변

1

당신은 어떤 서비스가없는

context.startActivity(startServiceIntent); 
+0

내 매니페스트가 맞습니까 ?? – slama007

+0

올바른 것처럼 보입니다. –

0

는, 당신은 활동을 열어야합니다. 만들기, 이제 브로드 캐스트 리시버를 확장 Autostart.java로라는 새 클래스를

public class AfterBootActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
}} 

:

Intent startServiceIntent = new Intent(context, ExampleActivity.class); 

context.startActivity(startServiceIntent); 
0

클래스 이름을 로 AfterBootActivity 만들기

public class Autostart extends BroadcastReceiver { 
    public void onReceive(Context context, Intent arg1) {   
     Intent intent = new Intent(context, StarterService.class); 
     context.startService(intent); 
}} 

Manifest 파일에서이 클래스를 수신자로 추가하십시오. 이 클래스는 부팅 시퀀스가 ​​완료된 후, 즉 휴대 전화가 시작된 후 Android OS가 전송하는 브로드 캐스트 통화를 수신합니다. 클래스 자동 시작은 안드로이드 OS에서 BOOT_COMPLETED 방송을 수신하면

public class StarterService extends Service { 
    private static final String TAG = "MyService"; 
    public IBinder onBind(Intent intent) { 
    return null; 
} 

public void onDestroy() { 
    Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 
} 

/** 
* The below started service opens the Activity. 
*/ 

public void onStart(Intent intent, int startid) { 
    Intent intents = new Intent(getBaseContext(), AfterBootActivity.class); 
    intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intents); 

    Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onStart"); 
}} 

그 다음 안드로이드 활동 "을 시작 StarterService를 시작합니다 :

이제 서비스가 확장됩니다 StarterService.java라는 이름으로 클래스를 만듭니다 AfterBootActivity "즉 메인 클래스. 우리는 오디오/비디오 또는 그 안에있는 모든 것을 재생할 수 있습니다.

Manifest.xml 아래을 변경

:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="on.boot.completed" 
    android:installLocation="internalOnly" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="7" 
     android:targetSdkVersion="18" /> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name="on.boot.completed.AfterBootActivity" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

     <receiver android:name="on.boot.completed.Autostart" > 
      <intent-filter> 
       <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

     <service 
      android:name="on.boot.completed.StarterService" 
      android:enabled="true" 
      android:exported="true" /> 
    </application> 

</manifest> 

또한 SD 카드에 앱을 설치 후 자동 시작이 작동하지 않을 경우 때문에 내부 메모리에 설치하는 것을 잊지 마십시오! 그래서 우리가 매니페스트에 추가하는 것이 중요합니다.

android:installLocation="internalOnly" 

모든 것이 앱을 실행합니다. 시작한 후 휴대 전화를 끄고 다시 켜면 기기가 부팅 된 후 앱이 자동으로 시작됩니다.

관련 문제