项目要求如下。应用程序的唯一任务是运行后台服务并与服务器同步数据或将一些信息上传到服务器也可能是一些通知。所以我想做的是让这个应用程序以编程方式从部署中不可见。任何人有线索请与我分享。也许它会显示在应用程序管理器中,但不应该在主屏幕上。
package com.example.drawpicdemo;
导入 java.util.ArrayList;导入 java.util.List;
导入android.app.ActionBar.LayoutParams;导入android.app.Activity;导入 android.content.pm.PackageManager;导入android.graphics.Color;导入android.os.Bundle;导入 android.view.Menu;导入android.view.MenuItem;导入 android.view.MotionEvent;导入android.view.View;导入 android.view.View.OnClickListener;导入 android.view.View.OnTouchListener;导入 android.widget.AdapterView;导入 android.widget.AdapterView.OnItemSelectedListener;导入 android.widget.ArrayAdapter;导入android.widget.Button;导入 android.widget.EditText;导入 android.widget.RelativeLayout;导入 android.widget.Spinner;
公共类 MainActivity 扩展 Activity 实现 OnClickListener{
Button red, green, blue, yellow, black;
int color = Color.RED;
DrawPictureView view;
public int StdStrokeWidth = 5;
Button EraseBtn, AddTextbtn;
Spinner sizespinner;
RelativeLayout parentlaLayout;
int dx, dy;
String textValue = "";
int lefttext = 0;
int toptext = 0;
int righttext = 0;
int bootomtext = 0;
public MainActivity() {
// TODO Auto-generated constructor stub
System.out.println("1111111111");
System.out.println("1111111111");
System.out.println("1111111111");
System.out.println("1111111111");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = new EditText(MainActivity.this.getApplicationContext());
System.out.println("1111111111");
System.out.println("1111111111");
//To hide the app icon
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
System.out.println("1111111111");
parentlaLayout = (RelativeLayout)findViewById(R.id.parentview);
EraseBtn = (Button)findViewById(R.id.erase);
AddTextbtn = (Button)findViewById(R.id.addtext);
EraseBtn.setOnClickListener(this);
AddTextbtn.setOnClickListener(this);
red = (Button) findViewById(R.id.red);
green = (Button)findViewById(R.id.green);
blue = (Button)findViewById(R.id.blue);
yellow = (Button)findViewById(R.id.yellow);
black = (Button)findViewById(R.id.black);
red.setBackgroundColor(Color.RED);
green.setBackgroundColor(Color.GREEN);
blue.setBackgroundColor(Color.BLUE);
yellow.setBackgroundColor(Color.YELLOW);
black.setBackgroundColor(Color.BLACK);
red.setOnClickListener(this);
green.setOnClickListener(this);
blue.setOnClickListener(this);
yellow.setOnClickListener(this);
black.setOnClickListener(this);
view = new DrawPictureView(MainActivity.this.getApplicationContext());
view.setDrawColor(color);
RelativeLayout parentView = (RelativeLayout)findViewById(R.id.drawview);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
parentView.addView(view, lp);
sizespinner = (Spinner)findViewById(R.id.sizespinner);
addItemsOnSpinner2();
sizespinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
sizespinner.getSelectedItem();
StdStrokeWidth = Integer.parseInt((String)sizespinner.getSelectedItem());
view.setStrokeWidth(StdStrokeWidth);
//sizespinner.setVisibility(View.INVISIBLE);
shouldClose = true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
text.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) text.getLayoutParams();
dx = X - lParams.leftMargin;
dy = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) text.getLayoutParams();
layoutParams.leftMargin = X - dx;
layoutParams.topMargin = Y - dy;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
text.setLayoutParams(layoutParams);
lefttext = layoutParams.leftMargin;
toptext = layoutParams.topMargin;
righttext = layoutParams.rightMargin;
bootomtext = layoutParams.bottomMargin;
textValue = text.getText().toString();
break;
}
return false;
}
});
text.setFocusable(true);
RelativeLayout.LayoutParams lptext = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
parentlaLayout.addView(text, lptext);
text.setVisibility(View.INVISIBLE);
}
boolean shouldClose = false;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
shouldClose = false;
addItemsOnSpinner2();
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
List<String> list = new ArrayList<String>();
for (int i = 8; i < 48; i++) {
list.add(Integer.toString(i));
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizespinner.setAdapter(dataAdapter);
sizespinner.setVisibility(View.VISIBLE);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.red:
color = Color.RED;
view.setDrawColor(color);
view.setStrokeWidth(StdStrokeWidth);
break;
case R.id.blue:
color = Color.BLUE;
view.setDrawColor(color);
view.setStrokeWidth(StdStrokeWidth);
break;
case R.id.green:
color = Color.GREEN;
view.setDrawColor(color);
view.setStrokeWidth(StdStrokeWidth);
break;
case R.id.yellow:
color = Color.YELLOW;
view.setDrawColor(color);
view.setStrokeWidth(StdStrokeWidth);
break;
case R.id.black:
color = Color.BLACK;
view.setDrawColor(color);
view.setStrokeWidth(StdStrokeWidth);
break;
case R.id.erase:
color = Color.WHITE;
view.setDrawColor(color);
view.setStrokeWidth(StdStrokeWidth + 50);
break;
case R.id.addtext:
if(AddTextbtn.getText().toString().equals("Add text"))
{
text.setVisibility(View.VISIBLE);
text.setHint("Add text here");
AddTextbtn.setText("Done");
}else
{
text.setVisibility(View.INVISIBLE);
AddTextbtn.setText("Add text");
int []pos = new int[]{lefttext, toptext, righttext, bootomtext};
view.setTextPosition(pos, textValue);
}
break;
default:
break;
}
}
EditText text ;
}