我正在尝试启动后启动和活动。但是应用程序正在崩溃。
给出错误
java.lang.RuntimeException:无法启动接收器com.example.shoky.onboot.OnBoot:android.content.ActivityNotFoundException:无法找到显式活动类
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);
}
}
答案 0 :(得分:0)
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
在receiver
内。包括&#34;类别android:name =&#34; android.intent.category.DEFAULT&#34;
在intent filter
内。
<intent-filter>
<action android:name= "android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
尝试在清单中更改以下内容
<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>
在OnBoot类中执行类似的操作
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);
}
}
答案 2 :(得分:0)
在onReceive方法中, 您创建的意图引用(myIntent)与您在startActivity(意图)中调用的内容不同。
注意:onReceive方法提供的意图用于过滤已注册的动作事件。 例如:
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{ `enter code here`
//code
}