2015年6月30日 星期二

Android-APP 啟動另一個 APP

用 APP 開啟另一個 APP
必須知道目標開啟的 Package  Name !!!

只需 Package Name ! 不是 MainAcitvity.java

 Intent intent = new Intent();
 PackageManager manager = getPackageManager();
 intent = manager.getLaunchIntentForPackage("com.example.activity");
 intent.addCategory(Intent.CATEGORY_LAUNCHER);
 startActivity(intent);

歡迎轉載,請註明出處。

Android-大量參考資料庫

ListView
連結位置 : 連結

Android-Parse DataBase 功能

查詢資料

ParseQuery<ParseObject> query = ParseQuery.getQuery("HELLO");
query.whereEqualTo("A", string);
query.getFirstInBackground(new GetCallback<ParseObject>() {
    public void done(ParseObject object, ParseException e) {
        if (object == null) {
            // 無資料
            Log.d("score", " not Retrieved the object.");
        } else {
            // 取得相對應資料
            Log.d("score", object.getString("NAME"));
        }
    }
});

Android-Parse Push 推波( Push )( 二 )

在上一篇有介紹如何匯入基本的Parse Jar
接下要介紹的是 : 推波 功能
  • 手機端發送推波
ParsePush push = new ParsePush(); 
push.setChannel("MyChannel"); 
push.setMessage("MyMessage"); 
push.sendInBackground();


2015年6月29日 星期一

Android-下拉更新視窗(Refresh ListView)

更新
20151213 : 新增 SwipeRefreshLayout 教學
20160318 : 新增 ListView 、GridView 更新問題

現在 UI 畫面不管是 ListView、 GridView 有做更新已經見怪不怪了
現在就來以原生的方法實作

1 . 把要更新的內容加載在 SwipeRefreshLayout 內
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.widget.SwipeRefreshLayout 
        android:id="@+id/swipe_container" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" >

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="3" >
        </GridView>
   
    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>


2 . 在 Activity 宣告並且監聽動作
package com.example.refreshview;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnRefreshListener{

    SwipeRefreshLayout mSwipeLayout;

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

        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);  
        mSwipeLayout.setOnRefreshListener(this);  
        mSwipeLayout.setColorSchemeColors(Color.RED);

    }

    @Override
    public void onRefresh() {

        // 模仿更新 ( 2秒
        Handler handler = new Handler();
        handler.postDelayed(new Runnable(){

            @Override
            public void run() {

                // 結束更新動畫
                mSwipeLayout.setRefreshing(false);
                Toast.makeText(MainActivity.this, "Refresh Success", Toast.LENGTH_SHORT).show();        

            }}, 2000);

    }
}


遇到的問題
當 ListView 、 GridView 向上滑動看資訊
但 向下滑動 竟然觸發到更新元件

解法 :
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {  
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {

  }

  @Override
  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    int topRowVerticalPosition = 
      (mListView == null || mListView.getChildCount() == 0) ? 
        0 : mListView.getChildAt(0).getTop();
    swipeContainer.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
  }
});


參考資料 : 如何使用下拉更新(SwipeRefreshLayout)
參考資料 : SwipeRefreshLayout with ListView done right

KeyWork : XListView
一般ListView
相關連結 : 位置

多種ListView GridView TextView
但要匯入SDK

相關連結 : 位置

看起來挺實用的但是限定AndroidStudio
實作過,超方便
但是下拉的圖示很尷尬..
Phoenix

2015年6月24日 星期三

Android-Parse Push 推波( Receiver )( 一 )

想必大家對推波不陌生吧
但距離上一次使用已經是1-2年前了
官方雖然已經出最新的SampleCode (AndroidStudio版本)
想必有些大大想要更精簡的版本吧

官方文件 : 連結
官方SDK : 連結

首先下載官方SDK

-----  匯入相對應的Jar -----
在SDK檔案底下找
重點是以下這兩個Jar( 只是Sample,非完整版本 ) :
1.9.2 版本
  1. bolts-android-1.2.0.jar
  2. Parse-1.9.2.jar 
  3. ParseCrashReporting-1.9.2.jar
1.10.2
  1. PPNS-1.10.2.jar
  2. compile 'com.parse:parse-android:1.10.1'
  3. compile 'com.parse.bolts:bolts-android:1.2.1'
1.10.3
    連結 : Box
-----  新增Application -----

public class MyApplication extends Application
{
    private static final String ApplicationID = "Your ID";
    private static final String ClientKey = "Your Key";

    @Override
    public void onCreate()
    {
        super.onCreate();
        Parse.initialize(this, ApplicationID, ClientKey);

        //initialized some stuff..
        
        ParsePush.subscribeInBackground("若加文字內容,代表指定接收頻道", 
            new SaveCallback()
        {

            @Override
            public void done(ParseException e)
            {
                if (null == e)
                {
                    ParseInstallation install = 
                        ParseInstallation.getCurrentInstallation();
                        
                    String token = install.getString("deviceToken");
                    //do some stuff with it...
                }
            }
        });
        ParseInstallation.getCurrentInstallation().saveInBackground();
        //some more stuff
    }
}

-----  AndroidManifest.xml 新增權限與服務 -----
  • 權限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
  • 服務與監聽
<service android:name="com.parse.PushService" />

<receiver android:name="com.parse.ParseBroadcastReceiver" >
    <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
         <action android:name="android.intent.action.USER_PRESENT" />
     </intent-filter>
</receiver>
<receiver
     android:name="com.parse.ParsePushBroadcastReceiver"
     android:exported="false" >
         <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.OPEN" />
                <action android:name="com.parse.push.intent.DELETE" />
          </intent-filter>
</receiver>

  • 註冊Application
<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    .
    .
    .

如果遇到在Parse上錯誤
裡面有 ParseLog SDK 可以使用

Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);

Parse.initialize(this, ApplicationID, ClientKey);

下一回會在介紹如果收到Parse Push如何處理。

參考資料 : PushService

歡迎轉載,請註明出處。