2016-05-24 5 views
1

Ich versuche zu starten und die Aktivität nach boot.App wird abgestürzt.Automatisch starten Sie eine Aktivität nach dem Start

Sein geben Fehler

java.lang.RuntimeException: Kann Empfänger com.example.shoky.onboot.OnBoot starten: android.content.ActivityNotFoundException: Kann nicht explizit Aktivitätsklasse

OnBoot.java 

    package com.example.shoky.onboot; 
    import android.util.Log; 
    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 

    /** 
    * Created by Shoky on 5/22/2016. 
    */ 
    public class OnBoot extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context,Intent intent){ 
       Log.w("ASHOK","ONBOOT"); 
       Intent myIntent = new Intent(context,MainActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(intent); 

     } 


    } 



AndroidManifest.xml 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.shoky.onboot" > 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <receiver android:name=".OnBoot" 
      android:enabled="true" 
      android:exported="true"> 

      <intent-filter> 
       <action android:name= "android.intent.action.BOOT_COMPLETED"/> 
      </intent-filter> 
     </receiver> 
     <activity 
      android:name=".MainActivity" 
      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> 

MainActivity.java 


    package com.example.shoky.onboot; 

    import android.app.Activity; 
    import android.support.v7.app.ActionBarActivity; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.view.MenuItem; 


    public class MainActivity extends Activity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_main, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 

      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 
    } 

Antwort

0

folgenden Versuchen Sie dieses in Ihrem Manifest

mit wechselnden
<receiver 
    android:name=".OnBoot" 
    android:enabled="true" 
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 

    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 

</receiver> 

Tun Sie so etwas in Ihrer OnBoot-Klasse

public void onReceive(Context context,Intent intent){ 
    Log.w("ASHOK","ONBOOT"); 
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 
     Intent myIntent = new Intent(context,MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intent); 
    } 

} 
0
    finden
  1. Hinzufügen android:permission="android.permission.RECEIVE_BOOT_COMPLETED" innerhalb receiver.
  2. einschließen. "Kategorie android: name =" android.intent.category.DEFAULT“ innen intent filter

      <intent-filter> 
           <action android:name= "android.intent.action.BOOT_COMPLETED"/> 
    
    <category android:name="android.intent.category.DEFAULT" /> 
          </intent-filter> 
         </receiver> 
    
0

Innen OnReceive Methode, Intent Referenz (myIntent) Sie erstellt haben, unterscheidet sich mit dem, was man bei startActivity anrufen (Absicht).

Hinweis: Mit der onReceive-Methode bereitgestellte Absicht wird verwendet, um das Aktionsereignis zu filtern, mit dem es registriert wurde. Beispiel:

if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
    { `enter code here` 
     //code 
    } 
Verwandte Themen