我正在尝试转换我在教程中找到的一些代码供我自己使用。最初,当用户单击我的应用程序生成的通知时,代码启动了系统联系人列表。我正在尝试启动我自己的Activity
而不是启动联系人列表,但它不起作用。更具体地说,没有任何反应没有错误,我的Activity
也没有加载。点击后通知窗口消失,原始Activity
仍然可见。
这是我的代码:
public class MyBroadcastReceiver extends BroadcastReceiver {
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
String deal = (String) extras.get("Deal");
String title = "Deal found at " + (String) extras.get("LocationName");
mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifyDetails = new Notification(R.drawable.icon, title,System.currentTimeMillis());
Class ourClass;
try {
ourClass = Class.forName("com.kjdv.gpsVegas.ViewTarget");
Intent startMyActivity = new Intent(context, ourClass);
PendingIntent myIntent = PendingIntent.getActivity(context, 0,startMyActivity, 0);
notifyDetails.setLatestEventInfo(context, title, deal, myIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
notifyDetails.flags |= Notification.DEFAULT_SOUND;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
这是我在AndroidManifext.xml
档案中的条目......
<activity android:name=".ViewTarget" android:label="@string/app_name" >
<intent-filter>
<action android:name="com.kjdv.gpsVegas.ViewTarget" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这是我要发布的Activity
......
public class ViewTarget extends ListActivity {
public ListAdapter getListAdapter() {
return super.getListAdapter();
}
public ListView getListView() {
return super.getListView();
}
public void setListAdapter(ListAdapter adapter) {
super.setListAdapter(adapter);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locations);
Log.v("db", "Inside ViewTarget");
}
}
答案 0 :(得分:23)
您运行的是哪个Android版本?您可能想尝试使用NotificationCompat。该课程包含在最新的支持包中。
Intent notificationIntent = new Intent(context, ViewTarget.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.app_icon)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.app_icon))
.setTicker(payload)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Message")
.setContentText(payload);
Notification n = builder.getNotification();
n.defaults |= Notification.DEFAULT_ALL;
nm.notify(0, n);
编辑: 我知道这是一个旧线程/问题,但这个答案帮助我在点击通知时显示活动。 对于那些不工作的人来说,可能是因为你没有注册&#34;清单中的活动。例如:
<activity
android:name="com.package.name.NameOfActivityToLaunch"
android:label="Title of Activity" >
<intent-filter>
<action android:name="com.package.name.NAMEOFACTIVITYTOLAUNCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
而且,希望这应该有效。 希望它有所帮助...
答案 1 :(得分:3)
我弄明白了这个问题。我忘了将包名包含在清单文件的活动声明中。
错:
activity android:name=".ViewTarget" android:label="@string/app_name"
正确:
activity android:name="com.kjdv.gpsVegas.ViewTarget" android:label="@string/app_name"
答案 2 :(得分:3)
你应该为Intent设置动作和类别。
Intent startMyActivity = new Intent(context, ourClass);
startMyActivity .setAction(Intent.ACTION_MAIN);
startMyActivity .addCategory(Intent.CATEGORY_LAUNCHER);
它有效
答案 3 :(得分:1)
您可以尝试删除Intent过滤器,因此它看起来像这样:
<activity android:name=".ViewTarget" android:label="@string/app_name" />
此外,不确定此代码是否有效:
ourClass = Class.forName("com.kjdv.gpsVegas.ViewTarget");
Intent startMyActivity = new Intent(context, ourClass);
你可以这样试试吗:
Intent startMyActivity = new Intent(context, ViewTarget.class);
答案 4 :(得分:1)
检查此代码
public class TestActivity extends Activity {
private static final int UNIQUE_ID = 882;
public static NotificationManager nm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent navigationIntent = new Intent();
navigationIntent.setClass(classname.this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
0);
String body = "New notificattion added!!!";
String title = "Notification";
Notification n = new Notification(R.drawable.icon, body,
System.currentTimeMillis());
//this is for giving number on the notification icon
n.number = Integer.parseInt(responseText);
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(UNIQUE_ID, n);
答案 5 :(得分:1)
要从Activity
发起Intent
,您需要添加一个标记:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
即使您在Intent
的构造函数中声明了类,也是如此。