分享意图后,留在屏幕上&不转移到选定的应用程序?

时间:2017-04-26 05:44:44

标签: android android-intent share-intent

我是Android初学者,不擅长英语,但让我来解释一下我的情况。

- 我想要的 -

在Chrome等网络浏览器中分享意图后,如果我按下我的应用程序。 我不想移动到我的应用程序

我只想在分享意图中选择我的应用后开始一些方法。

例如,我在共享列表中按我的应用程序后。我只想显示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

祝你有个愉快的一天。

1 个答案:

答案 0 :(得分:0)

您应该从活动中删除setContentView