java - 我做错了什么让我的应用程序在启动时运行?

标签 java android android-webview

我只是想让我的 webview 应用程序在 Android 设备启动时运行。我已经遵循了提到的每一个步骤in this post但是它不会在设备启动时运行。我在这里缺少什么?

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Toast.makeText(context, "Start Up", Toast.LENGTH_LONG).show();

            Intent serviceIntent = new Intent(context,
                    MainActivity.class)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(serviceIntent);
        } catch (Exception e) {
            Toast.makeText(context, "Start Up not possible!", Toast.LENGTH_LONG).show();
        }
    }

公共(public)类 MainActivity 扩展了 ActionBarActivity {

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

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());


    mWebView.loadUrl("http://www.google.com/");
    //Note: To detect when a URL has started and finished loading, use WebViewClient.onPageStarted and WebViewClient.onPageFinished.
}


@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);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

这是 list

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

    <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>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


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

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


</manifest>

最佳答案

您的接收器超出了您的应用程序元素,它应该位于内部

 <receiver android:name=".BootUpReceiver"> // i am suppose to be a child of the application
    <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>
 </application> // clossing application element

了解更多信息请查看here , and here

关于java - 我做错了什么让我的应用程序在启动时运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28421099/

相关文章:

Java解析ADIF文件

java - 未能延迟初始化角色 [] 的集合,无法初始化代理 - 无 session

java - Spring Boot使用http请求头进行分页

安卓 : AudioHardware pcm playback is going to standby

Android - webview 中断

android - 正在加载 Webview 是不是充满了设备屏幕?

android - 如何远程调试Crosswalk webview?

java - Android Picasso 未加载 img 以从处理程序查看

android - 我怎样才能在android中截取视频的屏幕截图

android - 如何更改 Dialog 的默认黑色暗淡背景 "color"(不是暗淡的数量)?