Java 如何使应用程序图标从主屏幕隐藏
本项目的要求如下。应用程序只有一个任务,即运行后台服务并与服务器同步数据,或者将一些信息上载到服务器,也可能是一些通知。所以我想做的是使这个应用程序在部署时以编程方式不可见。任何人有线索请与我分享。也许它会显示在应用程序管理器中,但不应该显示在主屏幕上Java 如何使应用程序图标从主屏幕隐藏,java,android,background-service,Java,Android,Background Service,本项目的要求如下。应用程序只有一个任务,即运行后台服务并与服务器同步数据,或者将一些信息上载到服务器,也可能是一些通知。所以我想做的是使这个应用程序在部署时以编程方式不可见。任何人有线索请与我分享。也许它会显示在应用程序管理器中,但不应该显示在主屏幕上 package com.example.drawpicdemo; 导入java.util.ArrayList; 导入java.util.List 导入android.app.ActionBar.LayoutParams; 导入android.a
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扩展活动实现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 ;
按钮红、绿、蓝、黄、黑;
int color=color.RED;
绘图视图;
公共int标准行程宽度=5;
按钮擦除BTN,添加文本BTN;
纺纱机;
相对性布局;
int-dx,dy;
字符串textValue=“”;
int lefttext=0;
int-toptext=0;
int righttext=0;
int bootomtext=0;
公共活动(){
//TODO自动生成的构造函数存根
System.out.println(“1111111”);
System.out.println(“1111111”);
System.out.println(“1111111”);
System.out.println(“1111111”);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=neweditText(MainActivity.this.getApplicationContext());
System.out.println(“1111111”);
System.out.println(“1111111”);
//隐藏应用程序图标
PackageManager p=getPackageManager();
p、 setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT\u启用\u状态\u禁用,PackageManager.DONT\u KILL\u应用程序);
System.out.println(“1111111”);
parentLayout=(RelativeLayout)findViewById(R.id.parentview);
EraseBtn=(按钮)findViewById(R.id.erase);
AddTextbtn=(按钮)findViewById(R.id.addtext);
橡皮擦btn.setOnClickListener(此);
AddTextbtn.setOnClickListener(此);
红色=(按钮)findViewById(R.id.red);
绿色=(按钮)findViewById(R.id.green);
蓝色=(按钮)findViewById(R.id.blue);
黄色=(按钮)findViewById(R.id.yellow);
黑色=(按钮)findViewById(R.id.black);
红色。背景颜色(颜色。红色);
绿色.setBackgroundColor(Color.green);
蓝色。背景色(颜色。蓝色);
黄色.背景色(颜色.黄色);
黑色.背景色(颜色.黑色);
红色。setOnClickListener(此);
green.setOnClickListener(this);
blue.setOnClickListener(这个);
黄色。setOnClickListener(此);
black.setOnClickListener(这个);
view=new DrawPictureView(MainActivity.this.getApplicationContext());
view.setDrawColor(颜色);
RelativeLayout parentView=(RelativeLayout)findViewById(R.id.drawview);
RelativeLayout.LayoutParams lp=新的RelativeLayout.LayoutParams(LayoutParams.FILL\u父级,LayoutParams.FILL\u父级);
parentView.addView(视图,lp);
SizeSpiner=(微调器)findViewById(R.id.SizeSpiner);
addItemsOnSpinner2();
sizespinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
//TODO自动生成的方法存根
sizeSpiner.getSelectedItem();
StdStrokeWidth=Integer.parseInt((字符串)sizeSpiner.getSelectedItem());
视图。设置行程宽度(标准行程宽度);
//SizeSpiner.setVisibility(视图不可见);
shouldClose=true;
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
//TODO自动生成的方法存根
final int X=(int)event.getRawX();
final int Y=(int)event.getRawY();
开关(event.getAction()&MotionEvent.ACTION\u掩码){
case MotionEvent.ACTION\u DOWN:
RelativeLayout.LayoutParams lParams=(RelativeLayout.LayoutParams)text.getLayoutParams();
dx=X-lParams.leftMargin;
dy=Y-lParams.topMargin;
打破
case MotionEvent.ACTION\u移动:
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();
打破
}
返回false;
}
});
text.setFocusable(true);
RelativeLayout.LayoutParams lptext=新的RelativeLayout.LayoutParams(LayoutParams.WRAP\u内容,LayoutParams.WRAP\u内容);
parentLayout.addView(text,lptext);
text.set
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
<category android:name="android.intent.category.LAUNCHER" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service
android:name=".AidlService"
android:process=":remote" >
<intent-filter>
<action android:name="com.remote.service.SERVER" />
</intent-filter>
</service>
</application>