当我打开手机时,如何在android中自动打开应用程序
目前,我正在进行一个项目,该项目获取我手机位置的位置和纬度,并将其发送到我开发应用程序的服务器。现在我想让我的应用程序在我打开手机时自动运行。它有一个用户名和密码活动,我想在打开手机时显示它当我打开手机时,如何在android中自动打开应用程序,android,authentication,android-asynctask,httpurlconnection,Android,Authentication,Android Asynctask,Httpurlconnection,目前,我正在进行一个项目,该项目获取我手机位置的位置和纬度,并将其发送到我开发应用程序的服务器。现在我想让我的应用程序在我打开手机时自动运行。它有一个用户名和密码活动,我想在打开手机时显示它 public class Login extends ActionBarActivity { EditText u_name; EditText password; Button login; TextView tv; ProgressDialog dialog = null; HttpURLConnect
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();
}
}
}
公共类登录扩展了ActionBarActivity{
编辑文本u__名称;
编辑文本密码;
按钮登录;
文本视图电视;
ProgressDialog=null;
httpurl连接连接;
最终字符串MyPREFERENCES=“MyPrefs”;
SharedReferences SharedReferences;
私有静态最终字符串TAG=“Login:”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
if(getIntent().getBooleanExtra(“退出”,false)){
完成();
}
//关闭所有活动,在此处完成
u_name=(EditText)findViewById(R.id.u_name);
密码=(EditText)findViewById(R.id.password);
login=(按钮)findviewbyd(R.id.login);
login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
字符串u=u_name.getText().toString().trim();
字符串p=password.getText().toString().trim();
Context=getApplicationContext();
Log.d(标记“用户名”+u+“密码”+p);
Toast.makeText(上下文,“用户名:+u,Toast.LENGTH_SHORT).show();
试一试{
布尔n=checkNetworkConnectivity();
如果(n==true){
Log.e(标签“等待验证……”);
登录检查(u,p);
Toast.makeText(上下文,“等待验证…”),Toast.LENGTH_LONG.show();
}否则{
Log.e(标签“无互联网连接”);
Toast.makeText(上下文,“无互联网连接”,Toast.LENGTH_LONG.show();
}
}捕获(例外e){
e、 printStackTrace();
}
试一试{
SharedReferences=getApplicationContext().GetSharedReferences(MyPREFERENCES,Context.MODE\u PRIVATE);
}捕获(例外e){
e、 printStackTrace();}
}
});
}
专用布尔值checkNetworkConnectivity(){
ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo activeNetwork=cm.getActiveNetworkInfo();
if(activeNetwork!=null&&activeNetwork.isConnected(){
返回true;
}否则{
返回false;
}
}
公共无效登录检查(字符串u,字符串p){
新逻辑检查()。执行(u,p);
}
类逻辑检查扩展了异步任务{
@凌驾
受保护的字符串背景(字符串…字符串){
字符串u=字符串[0];
字符串p=字符串[1];
字符串文本=空;
URL=null;
试一试{
url=新url(“https://www.urlOfThePage.com");
}捕获(格式错误){
e、 printStackTrace();
}
试一试{
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
连接设置读取超时(100000);
连接设置连接超时(150000);
conn.setDoInput(真);
连接设置输出(真);
Uri.Builder=新的Uri.Builder()
.appendQueryParameter(“u_名称”,u)
.appendQueryParameter(“密码”,p);
字符串查询=builder.build().getEncodedQuery();
OutputStream os=conn.getOutputStream();
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(os,“UTF-8”));
writer.write(查询);
writer.flush();
writer.close();
os.close();
连接();
BufferedReader=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
某人附加(行);
}
text=sb.toString();
}捕获(协议例外e){
e、 printStackTrace();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
如果(文本!=null){
返回文本;
}否则{
返回“”;
}
}
受保护的void onPostExecute(字符串结果){
试一试{
JSONObject resp=新JSONObject(结果);
字符串auth=resp.getString(“auth”);
int au=Integer.parseInt(auth);
字符串auth_msg=resp.getString(“auth_msg”);
如果(au==1){
stringuid=resp.getString(“uid”);
字符串u_name=resp.getString(“u_name”);
字符串密码=resp.getString(“密码”);
//Log.e(标签,“状态”+auth_msg);
Log.e(标签,“用户id:+uid”);
试一试{
SharedReferences.Editor=SharedReferences.edit();
putString(“uid”,uid);
编辑器.putString(“u_name”,u_name);
commit();
}
捕获(例外e){
Toast.makeText(getApplicationContext(),“首选项问题”,Toast.LENGTH_LONG.show();
}
startService(新意图(Login.this,LocationNdSend.class));
}
否则{
Toast.makeText(getApplicationContext(),“状态:+auth_msg,Toast.LENGTH_LONG).show();
}
}
捕获(JSONException e){
e、 printStackTrace();
Toast.makeText(getApplicationContext(),“json问题”,Toast.LENGTH_LONG.show();
}
}
}
}使用以下代码:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
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);
}
}
<receiver android:name=".BootReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>