在OnCreate()中调用MainActivity中的另一个类

时间:2015-12-11 10:15:14

标签: android android-manifest oncreate

我的活动启动应用程序中有两个主要类。

MainActivity.Java

package com.connect;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends Activity {

    private PolicyManager policyManager;

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

        policyManager = new PolicyManager(this);
        System.out.println("Hello");
        if (!policyManager.isAdminActive()) {
            Intent activateDeviceAdmin = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            activateDeviceAdmin.putExtra(
                    DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    policyManager.getAdminComponent());
            activateDeviceAdmin
                    .putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                            "After activating admin, you will be able to block application uninstallation.");
            startActivityForResult(activateDeviceAdmin,
                    PolicyManager.DPM_ACTIVATION_REQUEST_CODE);
        }
           }

       @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode == Activity.RESULT_OK
                && requestCode == PolicyManager.DPM_ACTIVATION_REQUEST_CODE) {
            // handle code for successfull enable of admin
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

    }
}

Droidian.java

package com.connect;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import java.io.IOException;

public class Droidian extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        PackageManager i = getApplicationContext().getPackageManager();
        i.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

        if (isMyServiceRunning() == false) {
            startService(new Intent(getApplicationContext(), DroidianService.class));
            Log.i("com.connect", "startService");
        }
    }

    private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (DroidianService.class.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

 }

执行MainActivity.java后,Droidian.java应该运行,因为这是将执行所有功能的类。我不知道如何在MainActivity.java类中调用另一个类。这是我的Manifest文件片段。

Android Manifest.xml

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

     <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" tools:ignore="OldTargetApi"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <supports-screens android:resizeable="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        />

    <application
        android:allowBackup="false"
        android:icon="@drawable/launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:persistent="true">
        <activity
            android:name="com.connect.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.connect.SampleDeviceAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />

            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
            </intent-filter>
        </receiver>

        <activity
            android:name="com.connect.Droidian"
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

3 个答案:

答案 0 :(得分:1)

您无法将两项活动声明为启动器。将你的机器人活动改为DEFAULT。

<activity
        android:name="com.connect.Droidian"
        android:excludeFromRecents="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>

答案 1 :(得分:0)

将此添加到您的onCreate

Intent intent = new Intent(getApplicationContext(), Droidian.class);
startActivity(intent);

答案 2 :(得分:0)

一些问题:
起初,Yogesh提到了发射器。意图过滤器中的启动器类别意味着此活动是首先被调用的活动,因此只能存在一个启动器。

目前还不清楚你是否真的希望Droidian成为一个Activity,因为你明白地称它为一个类。它是否意味着有自己的观点,还是应该是工人阶级?如果以后,不要扩展Activity,只需使它成为常规类。然后,您可以通过在MainActivity中实现它来调用函数,如numBins或使您的函数保持静态。

如果您真的打算使用Activity并“调用onCreate”,则必须启动Activity。某些方法无法手动启动,但在启动活动后会自动调用,请参阅the Android reference on Activities,尤其是“活动生命周期”部分。 启动活动的最简单方法是

Droidian mDroidian = new Droidian()

此外,您可能希望了解Android开发人员培训,尤其是the one about starting an activity