2017-04-26 65 views
-1

我是Android的初學者,不擅長英語,但讓我解釋一下我的情況。分享意圖後,留在屏幕上並不移動到選定的應用程序?

- 我想要什麼 -

從Web瀏覽器如Chrome份額的意圖後,如果我按我的應用程序。 我不想移動到我的APP

我只是想在開始一些方法後,我選擇我的應用程序共享意圖。

例如,我在共享列表中按我的應用程序後。我只想顯示Toast消息或調用方法。 (我想留在屏幕上,不想打開我的應用程序)

如果有人知道如何做到這一點。我會很感激。

的AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".activity.MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 


    <activity 
     android:name=".activity.GroupActivity" 
     android:launchMode="singleTask"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEND" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="image/*" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".activity.SettingsActivity"/> 

    <service android:name=".service.MyService"/> 
    <service android:name=".service.TopService"/> 
</application> 

MainActivity.class

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button createBtn = (Button)findViewById(R.id.main_create); 
     Button joinBtn = (Button)findViewById(R.id.main_join); 

     // create group 
     createBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showCreateDialog(); 
      } 
     }); 

     // join group 
     joinBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showJoinDialog(); 
      } 
     }); 

     System.out.println("Test--------------------"); 
    } 

    public void showCreateDialog() { 
     new MaterialDialog.Builder(this) 
       .title("그룹명을 입력하세요") 
       .inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME) 
       .positiveText("생성") 
       .input("Group 1", "Group 1", false, new MaterialDialog.InputCallback() { 
        @Override 
        public void onInput(@NonNull MaterialDialog dialog, CharSequence input) { 
         Toast.makeText(getApplicationContext(), "만들어진 그룹명은 "+input.toString()+" 입니다", Toast.LENGTH_SHORT).show(); 
         startActivity(new Intent(getApplicationContext(), GroupActivity.class)); 
        } 
       }).show(); 
    } 

    public void showJoinDialog() { 
     new MaterialDialog.Builder(this) 
       .title("고유키를 입력하세요") 
       .inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME) 
       .positiveText("참여") 
       .input("", "", false, new MaterialDialog.InputCallback() { 
        @Override 
        public void onInput(@NonNull MaterialDialog dialog, CharSequence input) { 
         Toast.makeText(getApplicationContext(), "고유키는 "+input.toString()+" 입니다", Toast.LENGTH_SHORT).show(); 
         startActivity(new Intent(getApplicationContext(), GroupActivity.class)); 
        } 
       }).show(); 
    } 
} 

GroupActivity的.java

public class GroupActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.group_activity); 

     initLayout(); 
     checkStart(); 

     System.out.println(" &&&&&&&&&&&&&&&&&&&&&&&&&&& Test 1 ###########################"); 

    } 

    // create menu in Toolbar 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_group, menu); 
     return true; 
    } 

    // event when settings icon pressed in Toolbar 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 

      case R.id.group_settings: 
       Toast.makeText(getApplicationContext(), "설정으로 이동", Toast.LENGTH_SHORT).show(); 
       startActivity(new Intent(getApplicationContext(), SettingsActivity.class)); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    // back button control 
    @Override 
    public void onBackPressed() { 

     new MaterialDialog.Builder(this) 
       .content("그룹에서 나가며, 히스토리가 모두 삭제됩니다. 계속하시겠습니까?") 
       .negativeText("NO") 
       .positiveText("YES") 
       .onPositive(new MaterialDialog.SingleButtonCallback() { 
        @Override 
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { 
         //// TODO: 2017. 4. 19. add delete group history action 

         Intent intent = new Intent(getApplicationContext(), MyService.class); 
         stopService(intent); 
         GroupActivity.super.onBackPressed(); 
        } 
       }) 
       .show(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     System.out.println("onDestroy called ============="); 
     Intent intent = new Intent(getApplicationContext(), MyService.class); 
     stopService(intent); 
    } 

    // start MyService.class to float always on Top Button when clipbard changed 
    public void checkStart() { 
     Intent intent = new Intent(getApplicationContext(), MyService.class); 
     startService(intent); 

     Toast toast = Toast.makeText(getApplicationContext(), "Start Clipboard Check", Toast.LENGTH_SHORT); 
     toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0); 
     toast.show(); 
    } 

    // make Toolbar, Tablayout, ViewPager 
    private void initLayout() { 

     Toolbar toolbar = (Toolbar) findViewById(R.id.group_toolbar); 
     toolbar.setTitle("Clipcon"); 
     setSupportActionBar(toolbar); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     tabLayout.addTab(tabLayout.newTab().setText("INFO")); 
     tabLayout.addTab(tabLayout.newTab().setText("HISTORY")); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

     final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
     MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager()); 
     viewPager.setAdapter(adapter); 
     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
     tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 

      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 
    } 

    // adapter for ViewPager to create two view (INFO, HISTORY) 
    public class MainPagerAdapter extends FragmentStatePagerAdapter { 

     private final int TAB_COUNT = 2; 
     private final int INFO = 0; 
     private final int HISTORY= 1; 

     public MainPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 

      switch (position) { 
       case INFO: 
        return new InfoFragment(); 
       case HISTORY: 
        return new HistoryFragment(); 
       default: 
        return null; 
      } 
     } 

     @Override 
     public int getCount() { 
      return TAB_COUNT; 
     } 
    } 
} 

InfoFragment.java

public class InfoFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_info, container, false); 

     String action = getActivity().getIntent().getAction(); 
     String type = getActivity().getIntent().getType(); 

     // when share image 
     if(Intent.ACTION_SEND.equals(action) && type != null){ 
      Uri uri = getActivity().getIntent().getParcelableExtra(Intent.EXTRA_STREAM); 
      //TopService.setUri(uri); 

      try { 
       ImageView testImage = (ImageView) view.findViewById(R.id.testImageView); 
       Bitmap bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri); 
       testImage.setImageBitmap(bm); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      // insert image uri to clipboard 
      ClipData clip = ClipData.newRawUri("test", uri); 
      ClipboardManager cm = (ClipboardManager)getActivity().getSystemService(Context.CLIPBOARD_SERVICE); 
      cm.setPrimaryClip(clip); 
     } 

     return view; 
    } 
} 

例如我想CopyCopy應用程序的功能作爲附加的圖像給出

 Example Image Functionality

Example Image Functionality

Example Image Functionality

有一個愉快的一天。

+0

把你的代碼放在這裏 –

+0

@BhupatBheda我添加了我的代碼(Manifest,MainActivity,GroupActivity,InfoFragment)。 Intent.ACTION_SEND在InfoFragment中。 – luckybox7

回答

0

您應該從活動中刪除setContentView

+0

哪些活動?以及如何將XML佈局文件附加到Activity類,如果我刪除setContentView? – luckybox7

+0

如果你不想顯示佈局,那麼你應該刪除setContentview –

+0

當我按下共享列表中的應用程序圖標時,我不想讓應用程序顯示。但它應該有佈局,當我以正常的方式啓動應用程序,如通過壁紙啓動應用程序。 (對於英文不好的抱歉) – luckybox7

相關問題