2014-09-19 118 views
-1

我希望我的應用程序在啓動平板電腦時啓動瀏覽器,但在啓動時崩潰。如果有人請讓我知道我的錯誤?我在activity_main.xmle中沒有寫任何內容。它顯示我的消息「不幸的是應用程序崩潰」。應用程序崩潰啓動時,使用BootReceiver

mainfest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.autostartmyapp" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" />  
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <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>  
     <receiver android:name=".BootReciever" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver>    
    </application>   
</manifest> 

mainActivitc類

package com.example.autostartmyapp;  

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 

public class MainActivity extends Activity {  
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com/")); 
     startActivity(browserIntent); 
    } 

    public class BootReciever extends BroadcastReceiver 
    {  
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 
      Intent myIntent = new Intent(context, MainActivity.class); 
      myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(myIntent); 
     } 

    }  
} 
+0

和你的logcat是什麼? – 2014-09-19 13:05:05

回答

1

分開兩個類和固定mainfest像下面

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.autostartmyapp" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" />  
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <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>  
     <receiver 
     android:name="com.example.autostartmyapp.BootReciever" 
     android:enabled="true"   > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver>    
    </application>   
</manifest> 

寫入MainActiity

MainActivity { 
...} 

BootReceiver類

package com.example.BootReciever; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
public class BootReciever extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context ctx, Intent arg1) { 

      Intent iMainActivity = new Intent(ctx, yourMainActivityClass); 
      iMainStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      ctx.startActivity(iMainActivity); 
    } 

}

0

在猜測,我會說這可能是因爲您的清單指定要.BootReceiver作爲接收器,但根據您的代碼BootReceiver類嵌套在MainActivity中。因此,您需要將BootReceiver移動到頂層類,或者更改清單以指定.MainActivity.BootReceiver作爲接收方。

如果這樣不能解決問題,請發佈您的logcat,以便我們看到導致崩潰的錯誤。

+0

它不起作用,logcat沒有錯誤。它通常運行正常,但問題是當啓動平板電腦 – logcat 2014-09-19 13:26:40

0

使用此的表現爲接收器部分:

<receiver 
     android:name=".BootReciever" 
     android:enabled="true" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 

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

照顧廣播接收機類的,應該是沒有內部:

package com.example.BootReciever; 

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

public class BootReciever extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context ctx, Intent arg1) { 

      Intent iMainActivity = new Intent(ctx, MainActivity.class); 
      iMainStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      ctx.startActivity(iMainActivity); 
    } 



} 
+0

對不起,它不起作用 – logcat 2014-09-19 13:27:30

+0

你的意思是沒有MainActivity啓動呢? 您是否將BootReciever作爲新課程移動? – 2014-09-19 13:39:28

+0

是的,我做到了,因爲你們建議...這裏是代碼,你可以複製它,並測試你自己?活動甚至沒有開始。 http://hastebin.com/fedebesesu.xml – logcat 2014-09-19 16:38:00

-1

你的應用程序啓動這麼快,不會讓其他重要的應用程序首先啓動我認爲你應該在你的應用程序中添加一些延遲來啓動。像這樣:

public class BootUpReceiver extends BroadcastReceiver { 

@Override 

public void onReceive(final Context context, Intent intent) { 

    // We can't wait on the main thread as it would be blocked if we wait for too long 
    new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       // Lets wait some time before starting the service to be fair to other processes on startup 
       Thread.sleep(5000); 
      } catch (InterruptedException e) { 
      } 

      Intent myIntent = new Intent(context, MainActivity.class); 
      myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(myIntent); 
     } 
    }).start(); 
} 
+0

它不起作用 – logcat 2014-09-19 13:26:10

+0

移動你的'BootReciever'到一個新的類,並在你的'manifast'補充一點:'<接收器的android:啓用=「真」的Android版本: name =「。BootReciever」android:permission =「android.permission.RECEIVE_BOOT_COMPLETED」>' – 2014-09-19 13:32:54

+0

是的,我是這麼做的,因爲你們建議......這裏是代碼,你可以複製它並測試你自己嗎?活動甚至沒有開始。 http://hastebin.com/fedebesesu.xml – logcat 2014-09-19 16:39:41