Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 重新启动手机或手动关闭或打开电源后启动活动_Android_Android Broadcastreceiver - Fatal编程技术网

Android 重新启动手机或手动关闭或打开电源后启动活动

Android 重新启动手机或手动关闭或打开电源后启动活动,android,android-broadcastreceiver,Android,Android Broadcastreceiver,如何在手机重启后启动启动活动或手动关闭或打开电源 我尝试了下面的代码,但它的工作只是为解锁手机,但我也想使它手动启动或重新启动手机完成 下面是代码 应用程序类 public class MyApplication extends Application { public static SharedPreferences preferences; public static SharedPreferences.Editor editor; public static Str

如何在手机重启后启动启动活动或手动关闭或打开电源

我尝试了下面的代码,但它的工作只是为解锁手机,但我也想使它手动启动或重新启动手机完成

下面是代码

应用程序类

public class MyApplication extends Application {
    public static SharedPreferences preferences;
    public static SharedPreferences.Editor editor;
    public static String PASSWORD = "password";
    public static String IS_SET = "nothing";
    public static String MYPREF = "mypref";
    public String TAG = getClass().getName();

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();  
   }

    public void init() {
        preferences = getSharedPreferences(MYPREF, 0);
        editor = preferences.edit();
        startService(new Intent(getBaseContext(), ScreenReceiver.class));

    }
}
public class ScreenReceiver extends BroadcastReceiver {
     SharedPreferences preferences;

        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println(intent.getAction());
            preferences = context.getSharedPreferences(MYPREF, 0);
            if (preferences != null) {
                String lock = preferences.getString(IS_SET, null);
                if (lock != null) {
                    if (lock.equals("passcode")) {
                        gotopasslock(context, intent);

                    } else {
                        gotopatternlock(context, intent);
                    }
                }
            }
        }
        public void gotopasslock(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                Intent intent1 = new Intent(context, Main.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                Intent myStarterIntent = new Intent(context, Main.class);
                myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myStarterIntent);
            }
        }

        public void gotopatternlock(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                Intent intent1 = new Intent(context, PatternLock.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                Intent myStarterIntent = new Intent(context, PatternLock.class);
                myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myStarterIntent);
            }
        }
    }
public class Main extends AppCompatActivity {
    private PinLockView mPinLockView;
    private IndicatorDots mIndicatorDots;
    private String TAG = getClass().getName();
    String oldPassword = "";
    String newPassword = "";
    SharedPreferences preferences;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // getSupportActionBar().hide();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_xml);
ScreenReceiver.class

public class MyApplication extends Application {
    public static SharedPreferences preferences;
    public static SharedPreferences.Editor editor;
    public static String PASSWORD = "password";
    public static String IS_SET = "nothing";
    public static String MYPREF = "mypref";
    public String TAG = getClass().getName();

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();  
   }

    public void init() {
        preferences = getSharedPreferences(MYPREF, 0);
        editor = preferences.edit();
        startService(new Intent(getBaseContext(), ScreenReceiver.class));

    }
}
public class ScreenReceiver extends BroadcastReceiver {
     SharedPreferences preferences;

        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println(intent.getAction());
            preferences = context.getSharedPreferences(MYPREF, 0);
            if (preferences != null) {
                String lock = preferences.getString(IS_SET, null);
                if (lock != null) {
                    if (lock.equals("passcode")) {
                        gotopasslock(context, intent);

                    } else {
                        gotopatternlock(context, intent);
                    }
                }
            }
        }
        public void gotopasslock(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                Intent intent1 = new Intent(context, Main.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                Intent myStarterIntent = new Intent(context, Main.class);
                myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myStarterIntent);
            }
        }

        public void gotopatternlock(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                Intent intent1 = new Intent(context, PatternLock.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                Intent myStarterIntent = new Intent(context, PatternLock.class);
                myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myStarterIntent);
            }
        }
    }
public class Main extends AppCompatActivity {
    private PinLockView mPinLockView;
    private IndicatorDots mIndicatorDots;
    private String TAG = getClass().getName();
    String oldPassword = "";
    String newPassword = "";
    SharedPreferences preferences;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // getSupportActionBar().hide();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_xml);
Main.class

public class MyApplication extends Application {
    public static SharedPreferences preferences;
    public static SharedPreferences.Editor editor;
    public static String PASSWORD = "password";
    public static String IS_SET = "nothing";
    public static String MYPREF = "mypref";
    public String TAG = getClass().getName();

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();  
   }

    public void init() {
        preferences = getSharedPreferences(MYPREF, 0);
        editor = preferences.edit();
        startService(new Intent(getBaseContext(), ScreenReceiver.class));

    }
}
public class ScreenReceiver extends BroadcastReceiver {
     SharedPreferences preferences;

        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println(intent.getAction());
            preferences = context.getSharedPreferences(MYPREF, 0);
            if (preferences != null) {
                String lock = preferences.getString(IS_SET, null);
                if (lock != null) {
                    if (lock.equals("passcode")) {
                        gotopasslock(context, intent);

                    } else {
                        gotopatternlock(context, intent);
                    }
                }
            }
        }
        public void gotopasslock(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                Intent intent1 = new Intent(context, Main.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                Intent myStarterIntent = new Intent(context, Main.class);
                myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myStarterIntent);
            }
        }

        public void gotopatternlock(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                Intent intent1 = new Intent(context, PatternLock.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                Intent myStarterIntent = new Intent(context, PatternLock.class);
                myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myStarterIntent);
            }
        }
    }
public class Main extends AppCompatActivity {
    private PinLockView mPinLockView;
    private IndicatorDots mIndicatorDots;
    private String TAG = getClass().getName();
    String oldPassword = "";
    String newPassword = "";
    SharedPreferences preferences;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // getSupportActionBar().hide();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_xml);
menifest

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

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="label">

        <meta-data
            android:name="com.google.android.gms.vision.DEPENDENCIES"
            android:value="face" />

        <activity android:name=".activities.ChooseActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <!--   <category android:name="android.intent.category.HOME" />
                   <category android:name="android.intent.category.DEFAULT" />-->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.Home" />
        <activity android:name=".activities.MainActivity" />
        <activity
            android:name=".activities.Main"
            android:showOnLockScreen="true" />
        <activity
            android:name=".activities.PatternLock"
            android:showOnLockScreen="true" />

        <!--<receiver
            android:name=".receiver.ScreenReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

        </receiver>-->
        <receiver
            android:name=".receiver.ScreenReceiver"
            android:label="ScreenReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
            <intent-filter>
                <category android:name="android.intent.category.HOME" />

                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <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>

缺少意图操作

要在重新启动后启动android活动,请在中添加以下行 AndroidManifest.xml

步骤1

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

第二步:

<receiver
    android:name=".receiver.ScreenReceiver"
    android:label="ScreenReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.USER_PRESENT"
    </intent-filter>
</receiver>


谷歌为此提供了大量有用的文档。基本要点是,你不必选择具体显示哪个广告,当发送广告请求时,谷歌会选择一堆合适的广告,并选择每次点击成本最高的广告。如果有人点击了那个广告,你就有收入了。@Sweeper,那么vungle视频广告呢?我提供的应用程序链接如果你有时间浏览链接,你能告诉我关于应用程序收益的情况吗?你有没有在ScreenReceiver的清单文件中包含action android.intent.action.BOOT_?你没有包含我将尝试@Krutik,但你能告诉我。。。。。我想禁用home按钮(软键和硬键)、back按钮,甚至用户也无能为力(注意:我想问你所有关于实现类似锁应用程序的事情)参见编辑的问题,,我已经实现了你的答案,但当我重新启动设备时,锁屏出现一分钟后,我想在设备重新启动后准确显示…舔内置的手机锁工作。。我还想禁用设备内置锁