2013-09-27 102 views
0

該項目的要求如下。應用程序只具有運行後臺服務和與服務器同步數據的任務,或者將一些信息上傳到服務器也可能是一些通知。所以我想要做的是使這個應用程序從部署中以編程方式隱形。任何人都有線索請與我分享。也許它會顯示在應用程序管理器中,但不應該在主屏幕上。如何使主屏幕隱藏應用程序圖標

package com.example.drawpicdemo; 

import java.util.ArrayList; import java.util.List;

import android.app.ActionBar.LayoutParams; import android.app.Activity; import android.content.pm.PackageManager; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.RelativeLayout; import 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 ; 

}

+0

..如果您從清單文件類別意圖過濾器那麼它將不會顯示在主屏幕上 – PankajAndroid

+0

@AndroidDev在這種情況下,應用程序能否正常工作。像後臺服務,它會被調用嗎? –

+0

粘貼您的清單和佈局。 –

回答

1

把下面的代碼相同,啓動圖標將不可見。

PackageManager p = getPackageManager(); 
     p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 

將上面的代碼放到主屏幕的onCreate()方法中。下面從清單文件的MainActivity線

+0

我刪除了意圖過濾器,並在啓動活動中寫入了此代碼。是的圖標被刪除,但它看起來像功能沒有工作,即使我試圖打印一些控制檯s.o.p.l.語句但未在控制檯中顯示。 –

+0

你是否正確地開始了你的服務?請在這裏發佈你的代碼。並接受答案,以便其他人發現這個有用。 –

+0

不,對於演示我沒有創建任何服務,但只是活動和寫了一些系統日誌,但沒有任何顯示在控制檯中。 –

0

刪除,,

<category android:name="android.intent.category.LAUNCHER" /> 
0

你可以試一下這個:你要隱藏的應用程序圖標

<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> 
相關問題