如何在我打开 phone 时自动打开 android 中的应用程序

How to open an application in android automatically when I switch on my phone

目前我正在做一个项目,它获取我的 phone 位置的位置和纬度,并将其发送到我开发应用程序的服务器。现在我想在我打开手机时自动让我的应用 运行 。它有一个用户名和密码 activity,我想在我打开 phone.

时显示它
public class Login extends ActionBarActivity {
EditText u_name;
EditText password;
Button login;
TextView tv;
ProgressDialog dialog = null;
HttpURLConnection conn;
final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedpreferences;
private static final String TAG = "Login:";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
    }
    //close all activity finish here

    u_name = (EditText) findViewById(R.id.u_name);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String u = u_name.getText().toString().trim();
            String p = password.getText().toString().trim();
            Context context = getApplicationContext();
            Log.d(TAG, "username" + u + "password:" + p);
            Toast.makeText(context, "username: " + u, Toast.LENGTH_SHORT).show();

            try {
                boolean n = checkNetworkConnectivity();
                if (n == true) {
                    Log.e(TAG,"Wait Authenticating.........");
                    login_check(u, p);
                    Toast.makeText(context, "Wait Authenticating.......", Toast.LENGTH_LONG).show();
                } else {
                    Log.e(TAG,"No Internet Connection");
                    Toast.makeText(context, "No Internet connection", Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try{
                sharedpreferences=getApplicationContext().getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
            }catch(Exception e){
                e.printStackTrace();}

        }
    });
}

private boolean checkNetworkConnectivity() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    } else {
        return false;
    }
}
public void login_check(String u, String p) {
    new logi_check().execute(u, p);
}
class logi_check extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... strings) {
        String u = strings[0];
        String p = strings[1];
        String text = null;
        URL url = null;
        try {
            url = new URL("https://www.urlOfThePage.com");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(100000);
            conn.setConnectTimeout(150000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("u_name", u)
                    .appendQueryParameter("password", p);
            String query = builder.build().getEncodedQuery();
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();
            conn.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) {

                sb.append(line);
            }
            text = sb.toString();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (text != null) {
            return text;
        } else {
            return "";
        }
    }
    protected void onPostExecute(String result){

        try{
            JSONObject resp = new JSONObject(result);
            String auth= resp.getString("auth");
            int au=Integer.parseInt(auth);
            String auth_msg = resp.getString("auth_msg");

            if(au==1) {
                String uid= resp.getString("uid");
                String u_name= resp.getString("u_name");
                String password=resp.getString("password");
                //Log.e(TAG,"status" + auth_msg);
                Log.e(TAG,"User id:" + uid);


                try{
                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putString("uid",uid);
                    editor.putString("u_name",u_name);
                    editor.commit();
                }

                catch(Exception e){
                    Toast.makeText(getApplicationContext(),"preferences problem",Toast.LENGTH_LONG).show();
                }
                startService(new Intent(Login.this,LocationNdSend.class));
            }

            else{
                Toast.makeText(getApplicationContext(), "Status:" + auth_msg, Toast.LENGTH_LONG).show();
            }
        }

        catch (JSONException e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"json problem",Toast.LENGTH_LONG).show();
        }
    }
}

}

使用下面的代码:

第一步:在AndroidManifest.xml

中设置权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

第 2 步:创建广播接收器并启动您的 activity

public class BootReciever extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent(context, Tabs.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);
}

}

第 3 步:在接收器中添加 this is intent 过滤器,

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