1. 程式人生 > >Android單擊通知欄後返回正在執行的程式並跳轉到指定fragment頁面

Android單擊通知欄後返回正在執行的程式並跳轉到指定fragment頁面

今天寫通知欄的時候遇到了這樣的一個問題:在fragment中傳送通知,點選通知後跳轉到指定的fragment。

第一思路就是建立一個通知,點選通知後啟動activity,在activity接受傳入過來的值來判斷啟動那個fragment。

既然有思路了,就開始寫吧。程式碼如下:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
="match_parent" android:layout_height="match_parent">
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_index"/> </LinearLayout>

MainActivity.class:

public class MainActivity extends AppCompatActivity
{
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_index,new NotifyFragment()).commit(); } @Override
protected void onResume() { Intent intent = getIntent(); String value = intent.getStringExtra("toValue"); Log.i("TAG", "onResume: " + value); if(!TextUtils.isEmpty(value)) { switch (value) { case "href": getSupportFragmentManager().beginTransaction().replace(R.id.fragment_index, new HrefFragment()).commit(); break; } } super.onResume(); } }

NotifyFragment.class:

    public class NotifyFragment extends Fragment {

    private View view;

    private Button btnNotify;

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

        btnNotify = (Button) view.findViewById(R.id.btn_notify);

        btnNotify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

                    Intent intent = new Intent(getContext(),MainActivity.class);

                    intent.putExtra("toValue","href");

                    PendingIntent pend = PendingIntent.getActivity(getContext(),201,intent,PendingIntent.FLAG_UPDATE_CURRENT);

                    Notification notify = new Notification.Builder(getContext())
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("這是標題")
                            .setContentText("這是內容")
                            .setContentIntent(pend)
                            .build();

                    notify.flags = Notification.FLAG_AUTO_CANCEL;

                    NotificationManager manager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);

                    manager.notify(1,notify);
                }

            }
        });

        return view;
    }
}

fragment_notify.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_notify"
        android:textSize="16sp"
        android:text="傳送通知"
        />

</LinearLayout>

跳轉後的fragment

public class HrefFragment extends Fragment{

    private View view;

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

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/text_href"
        android:textSize="20sp"
        android:text="跳轉後的介面"
        android:gravity="center"
        />
</LinearLayout>

執行結果:

05-27 00:15:38.040 17430-17430/com.example.administrator.notifydemo I/TAG: onResume: href

1.png-14.7kB
成功接受到結果,頁面也跳轉了,但是單擊返回鍵的時候會返回上個activity。正常的思路應該是在當前程式裡點選通知直接跳轉到指定fragment上,這樣寫不符合要求。想了下,是不是沒有設定flag的原因?
結合網上資料後代碼如下:

btnNotify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

                    //Intent intent = new Intent(getContext(),MainActivity.class);
                    //判斷當前activity是否建立
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.putExtra("toValue","href");

                    //====增加部分
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);

                    intent.setComponent(new ComponentName(getContext(), MainActivity.class));

                    intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

                    PendingIntent pend = PendingIntent.getActivity(getContext(),201,intent,PendingIntent.FLAG_UPDATE_CURRENT);

                    Notification notify = new Notification.Builder(getContext())
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("這是標題")
                            .setContentText("這是內容")
                            .setContentIntent(pend)
                            .build();

                    notify.flags = Notification.FLAG_AUTO_CANCEL;

                    NotificationManager manager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);

                    manager.notify(1,notify);

                }

            }
        });

執行效果如下:
GIF.gif-702.8kB
但是這樣又出現一個問題:在軟體介面上點選是沒有問題,但是無法接受到傳過來的引數,退出去後點通知能夠接受到引數。也就是說onResume()方法沒有被執行。

 <activity android:name=".MainActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

當使用Intent來啟動Activity時會呼叫onNewIntent()方法。
MainActivity:

    @Override
    protected void onResume() {
        //退出應用後點擊通知欄進去是進行這個方法
        getNotify(getIntent());
        super.onResume();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        //在應用上點選通知欄進去是執行這個方法
        getNotify(intent);
        setIntent(intent);
    }

    private void getNotify(Intent intent){
        String value = intent.getStringExtra("toValue");

        Log.i("TAG", "onNewIntent: " + value);

        if(!TextUtils.isEmpty(value)) {
            switch (value) {
                case "href":
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_index, new HrefFragment()).commitAllowingStateLoss();
                    //這裡不是用的commit提交,用的commitAllowingStateLoss方式。commit不允許後臺執行,不然會報Deferring update until onResume 錯誤
                    break;
            }
        }
        super.onNewIntent(intent);
    }

NotifyFragment的Intent修改如下

Intent intent = new Intent(getContext(),MainActivity.class);
intent.putExtra("toValue","href");

最後執行情況:

GIF0.gif-6372.6kB

ok,三種開啟方式都能進入進入頁面。問題解決。