2014-09-19 29 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